DATA Step, Macro, Functions and more

create a new variable with same value

Reply
Frequent Contributor
Frequent Contributor
Posts: 101

create a new variable with same value

[ Edited ]
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).?

Super User
Posts: 6,946

Re: create a new variable with same value

[ Edited ]
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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Trusted Advisor
Posts: 1,131

Re: create a new variable with same value

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
Super User
Posts: 9,682

Re: create a new variable with same value


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;
Ask a Question
Discussion stats
  • 3 replies
  • 283 views
  • 0 likes
  • 4 in conversation