我想知道我是否可以在Macro Loop中做到这一点。
例如,这是tableA
ID -- Fruit -- Count
1 Banana 1
1 Strawberry 2
1 Apple 3
1 Blueberries 4
2 Apple 1
3 Strawberry 1
3 Apple 2
Run Code Online (Sandbox Code Playgroud)
我通常不使用SAS MACRO这样做
proc sql;
select ID,t2.fruit AS fruit1,
t3.fruit AS fruit2,
.
.
.
from core_table t1
LEFT JOIN TableA t2 on t2.id = t1.id AND t2.count=1
LEFT JOIN TableA t3 on t3.id = t1.id AND t3.count=2
.
.
.
.
Run Code Online (Sandbox Code Playgroud)
所以输出就像
ID Fruit1 Fruit2 Fruit3 Fruit4
1 Banana Strawberry Apple Blueberries
2 Apple
3 Strawberry Apple
Run Code Online (Sandbox Code Playgroud)
基本上,输出会使变量标准化。所以我想我可以通过使用Do循环来完成此任务,我一直在谷歌搜索,但是我不知道该怎么做。
谢谢
No need for a macro for something like that. Just use PROC TRANSPOSE.
First let's convert your listing into an actual SAS dataset so we have something to test with.
data have ;
input id fruit :$20. count;
cards;
1 Banana 1
1 Strawberry 2
1 Apple 3
1 Blueberries 4
2 Apple 1
3 Strawberry 1
3 Apple 2
;
Run Code Online (Sandbox Code Playgroud)
Here is the PROC TRANSPOSE code to convert it.
proc transpose data=have out=want prefix=Fruit;
by id;
var fruit;
id count;
run;
Run Code Online (Sandbox Code Playgroud)
Results:
Obs id _NAME_ Fruit1 Fruit2 Fruit3 Fruit4
1 1 fruit Banana Strawberry Apple Blueberries
2 2 fruit Apple
3 3 fruit Strawberry Apple
Run Code Online (Sandbox Code Playgroud)