Macros 如何记录SAS的移动方向?

Macros 如何记录SAS的移动方向?,macros,sas,sas-macro,Macros,Sas,Sas Macro,我有一个像这样的迷宫文件 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 我怎样才能像这样记录从开始到结束的移动方向 dir

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

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;
然而,它只是没有按照正确的移动顺序


如果你能帮忙,谢谢你。

这里有一种方法。使用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;

请更详细地描述您试图对数组执行的操作,并查看array语句的文档。请举例说明从给定的一组输入中预期的输出应该是什么样的。从您提供的信息中不清楚您想要的是“迷宫文件”是一个迷宫,其中1是墙,0是可移动的空间?(如果是这样,你的例子是无法解决的,除非你可以对角移动)。您想要什么作为输出?从开始到结束的路径列表?请显示所需的输出数据集。很抱歉在迷宫文件中输入错误。是的,1表示墙,0表示可移动的空格。谢谢回答我的问题。是否可以将任务(起点和终点)存储在散列中,并设置起点和终点,然后循环直到当前点等于终点?是的,但如果只有2个点,并且可以直接从变量获取它们,这似乎有些过分。哦,这是真的。我只是想在迷宫或任务发生变化时防止硬代码。自己动手做吧,如果卡住了,再发一个问题。