BookmarkSubscribeRSS Feed
ari
Quartz | Level 8 ari
Quartz | Level 8
Have:   
ID v1v2v3
1-3aa
1-2bb
1-1cb
11db
2-20aa
2-6bb
2-3cb
2-2db
31aa

 

ID v1v2v5
1-3aa
1-2ba
1-1ca
11da
2-20aa
2-6ba
2-3ca
2-2da
31aa

 

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).?

3 REPLIES 3
Kurt_Bremser
Super User
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

Jagadishkatam
Amethyst | Level 16
data want;
do until(first.id);
set have;
by id; v5=v2; end; do until(first.id); set have;
by id; output; end; run;
Thanks,
Jag
Ksharp
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 3130 views
  • 0 likes
  • 4 in conversation