| 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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.