BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

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;
1 ACCEPTED SOLUTION

Accepted Solutions
rudfaden
Lapis Lazuli | Level 10

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;

View solution in original post

5 REPLIES 5
rudfaden
Lapis Lazuli | Level 10

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;
Kurt_Bremser
Super User

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;
gamotte
Rhodochrosite | Level 12

Hello,

 

Another solution without array :

data wanted;
set have2;
by ID;
if not first.id then call missing(of y:);
run;
PaigeMiller
Diamond | Level 26
IF not first.ID then call missing(of y1-y5);

No loops at all.

--
Paige Miller
rudfaden
Lapis Lazuli | Level 10

Very nice. I like this version better than mine 🙂

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1099 views
  • 8 likes
  • 5 in conversation