I wonder I can do this in Macro Loop.
For example, this is 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
I normally do like this not using 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
.
.
.
.
so Output is be like
ID Fruit1 Fruit2 Fruit3 Fruit4
1 Banana Strawberry Apple Blueberries
2 Apple
3 Strawberry Apple
Basically,Output will denormalise the variables. SO I am guessing I can do this task by using Do loop, I have been googling around but I can not figure out how to do.
Thanks
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
;
Here is the PROC TRANSPOSE code to convert it.
proc transpose data=have out=want prefix=Fruit;
by id;
var fruit;
id count;
run;
Results:
Obs id _NAME_ Fruit1 Fruit2 Fruit3 Fruit4
1 1 fruit Banana Strawberry Apple Blueberries
2 2 fruit Apple
3 3 fruit Strawberry Apple