Have: | |||
ID | v1 | v2 | v3 |
1 | -3 | a | a |
1 | -2 | b | b |
1 | -1 | c | b |
1 | 1 | d | b |
2 | -20 | a | a |
2 | -6 | b | b |
2 | -3 | c | b |
2 | -2 | d | b |
3 | 1 | a | a |
ID | v1 | v2 | v5 |
1 | -3 | a | a |
1 | -2 | b | a |
1 | -1 | c | a |
1 | 1 | d | a |
2 | -20 | a | a |
2 | -6 | b | a |
2 | -3 | c | a |
2 | -2 | d | a |
3 | 1 | a | a |
I am wondering how to create a new variable v5, if any of the values of v3 per subject are 'a' then v5 should be populated with the same value 'a' (v5 contains only a in output).?
proc sort
data=have (keep=id v3 where=(v3 = 'a'))
out=help (rename=(v3=v5))
nodupkey
;
by id;
run;
data want;
merge
have (drop=v3)
help
;
by id;
run;
Just one of the many possible solutions
data want;
do until(first.id);
set have;
by id;
v5=v2;
end;
do until(first.id);
set have;
by id;
output;
end;
run;
data have; infile cards expandtabs truncover; input ID v1 v2 $ v3 $; cards; 1 -3 a a 1 -2 b b 1 -1 c b 1 1 d b 2 -20 a a 2 -6 b b 2 -3 c b 2 -2 d b 3 1 a a ; run; proc sql; select *,case when(sum(v3='a') ne 0) then 'a' else ' ' end as v5 from have group by id; quit;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.