Hello
I want that for each ID:
in first raw values of Y1-Y5 will be non-null .
in non-first raw values of Y1-Y5 will be-null .
In real data there are 100 Y variables (Y1,Y2,........Y100).
What is the way to write the following statements in macro or array?
IF not first.ID then do;
Y1=.;
Y2=.;
Y3=.;
Y4=.;
Y5=.;
end;
Data have;
input ID X $ Y1 Y2 Y3 Y4 Y5;
cards;
3 B 5 3 1 2 1
3 C 1 4 2 3 1
1 A 1 2 3 4 5
1 B 2 3 2 4 1
1 C 3 1 2 1 2
2 A 1 3 2 4 5
2 C 2 5 4 3 2
4 B 1 3 2 1 4
;
run;
proc sort data=have out=have2;
by ID;
Run;
data wanted;
set have2;
by ID;
IF not first.ID then do;
Y1=.;
Y2=.;
Y3=.;
Y4=.;
Y5=.;
end;
Run;
I don't see why you would need a macro. But an array can do it like this:
data wanted;
set have2;
by ID;
array y (5) y1-y5;
IF not first.ID then do;
do i=1 to 5;
y(i)=.;
end;
end;
Run;
I don't see why you would need a macro. But an array can do it like this:
data wanted;
set have2;
by ID;
array y (5) y1-y5;
IF not first.ID then do;
do i=1 to 5;
y(i)=.;
end;
end;
Run;
If all your variables to be set start with the same prefix, you can suimplify @rudfaden 's suggestion:
data wanted;
set have2;
by ID;
array my_y (*) y:;
if not first.ID then do i = 1 to dim(my_y);
my_y(i) = .;
end;
run;
Hello,
Another solution without array :
data wanted;
set have2;
by ID;
if not first.id then call missing(of y:);
run;
IF not first.ID then call missing(of y1-y5);
No loops at all.
Very nice. I like this version better than mine 🙂
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.