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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2289 views
  • 0 likes
  • 4 in conversation