data one;
input id par $ ady;
datalines;
1 a 3
1 a 6
1 a 9
2 a 6
3 a 3
3 a 4
4 a 4
4 a 8
5 a 7
5 a 10
6 a 5
6 a 12
;
Dear
I need to create a variable 'want' based on 'ady' variable values.
If ady contains at least one of the values (3,6,9,12 or 15,18,21,24) in any record by id and par then for the subject want='Yes'; else want='No'
output needed;
id want
1 Yes
2 Yes
3 Yes
4 No
5 No
6 Yes
for id= 4 and 5 the subjects do not have records with values (3,6,9,12 or 15). So want='No'.
Please suggest. Thank you
data one;
input id par $ ady;
datalines;
1 a 3
1 a 6
1 a 9
2 a 6
3 a 3
3 a 4
4 a 4
4 a 8
5 a 7
5 a 10
6 a 5
6 a 12
;
data want;
set one;
by id par;
length want $3;
retain want;
if first.par then want='no';
if ady in (3,6,9,12,15,18,21,24) then want='yes';
if last.par;
keep id par want;
run;
HI Thank you very much for the help. I have another question which i need help. Currently in my data, I see 3 interval up to 24. If in my data contains any subject with greater than 24(for example, 39), instead of me again updating my code, is there any way i can make my code efficient. Please suggest solution
Thank you
You apparently desire want='yes' for any ID/PAR that has at least one record with ADY as a multiple of 3. I.e. ADY divided by 3 has a remainder of zero. In other words ADY modulo 3 = 0, which in SAS language is MOD(ADY,3)=0.
data one;
input id par $ ady;
datalines;
1 a 3
1 a 6
1 a 9
2 a 6
3 a 3
3 a 4
4 a 4
4 a 8
5 a 7
5 a 10
6 a 5
6 a 12
;
data want (keep=id par want);
set one (where=(mod(ady,3)=0) in=inw)
one (where=(mod(ady,3)^=0)) ;
by id par;
if first.par;
if inw then want='Yes';
else want='No';
run;
This program reads dataset ONE in two "substreams" of the SET statement. One stream reads in only records with ADY divisible by 3. The other reads all the other records. The SET statement with a "by id par" interleaves these two streams such that any qualifying ADY is at the start of each ID/PAR group. So all you have to do is test the first incoming record for each ID/PAR.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: