【问题标题】:how to record the moving direction is SAS?如何记录移动方向是SAS?
【发布时间】:2017-05-03 03:29:54
【问题描述】:

我有一个像这样的迷宫文件。

1111111
1001111
1101101
1101001
1100011
1111111

表示方向的格式$direction

start end label
D     D    down
L     L    left
R     R    right
U     U    up

然后,我有一个指示起点和终点的数据集。

Row   Column
start  2      2
end    3      6

我怎样才能像这样记录从开始到结束的移动方向?

direction  row column
           2    2
right      2    3
down       3    3      
down       4    3
down       5    3

我用过数组

array m(i,j)
if  m(i,j) = 0 then
row=i;
column=j;
output;

但是,它只是没有按照正确的移动顺序。

如果你能帮忙,谢谢。

【问题讨论】:

  • 请更详细地描述您要对数组执行的操作,并查看数组语句的文档。
  • 举一个例子,说明你的预期输出应该是什么样子的给定输入集。从您提供的内容中不清楚您在寻找什么
  • 那个“迷宫文件”是一个迷宫,其中 1 是墙壁,0 是可移动的空间? (如果是这样,除非您可以沿对角线移动,否则您的示例是无法解决的)。你想要什么作为输出?从头到尾的路径列表?请显示所需的输出数据集。
  • 抱歉迷宫文件中有错字。是的,1 是指墙壁,0 是可移动的空间。

标签: macros sas sas-macro


【解决方案1】:

这是执行此操作的一种方法。使用 SAS 数据步骤逻辑编写更通用的迷宫求解算法留给读者作为练习,但这应该适用于迷宫。

/* Define the format */

proc format;
 value $direction
 'D' = 'down'
 'L' = 'left'
 'R' = 'right'
 'U' = 'up'
;
run;


data want;

/*Read in the maze and start/end points in (y,x) orientation*/
array maze(6,7) (
1,1,1,1,1,1,1,
1,0,0,1,1,1,1,
1,1,0,1,1,0,1,
1,1,0,1,0,0,1,
1,1,0,0,0,1,1,
1,1,1,1,1,1,1
);
array endpoints (2,2) (
2,2
3,6
);
/*Load the start point and output a row*/
x = endpoints(1,2);
y = endpoints(1,1);
output;

/*
 Navigate through the maze.
 Assume for the sake of simplicity that it is really more of a labyrinth,
 i.e. there is only ever one valid direction in which to move, 
 other than the direction you just came from,
 and that the end point is reachable
*/
do _n_ = 1 by 1 until(x = endpoints(2,2) and y = endpoints(2,1));
    if maze(y-1,x) = 0 and direction ne 'D' then do;
        direction = 'U';
        y + -1;
    end;
    else if maze(y+1,x) = 0 and direction ne 'U' then do;
        direction = 'D';
        y + 1;
    end;
    else if maze(y,x-1) = 0 and direction ne 'R' then do;
        direction = 'L';
        x + -1;
    end;    
    else if maze(y,x+1) = 0 and direction ne 'L' then do;
        direction = 'R';
        x + 1;
    end;            
    output;
    if _n_ > 15 then stop; /*Set a step limit in case something goes wrong*/
end;
format direction $direction.;
drop maze: endpoints:;
run;

【讨论】:

  • 感谢您回答我的问题。是否可以将任务(起点和终点)存储在哈希中并设置起点和终点然后循环直到当前点等于终点?
  • 是的,但是当你只有 2 个 point 并且你可以直接从变量中获取它们时,这似乎有点过头了。
  • 哦,原来如此。一旦迷宫或任务发生变化,我只想防止硬编码。
  • 自己尝试一下,如果遇到困难,请发布另一个问题。
猜你喜欢
  • 2019-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多