Hi,
I am trying to create the dummy variable Want which is = 1 if there is any observation that has a value in Have.
E.g. Want = 1 for all observations of A1 because there is at least one observation with a value in Have. Want = 0 for B1.
Is there a way to code this? Thanks for your help!
Sample data:
ID | Have | Want |
A1 | . | 1 |
A1 | . | 1 |
A1 | 100 | 1 |
A1 | . | 1 |
A1 | . | 1 |
A1 | . | 1 |
A1 | . | 1 |
A1 | . | 1 |
B1 | . | 0 |
B1 | . | 0 |
B1 | . | 0 |
B1 | . | 0 |
B1 | . | 0 |
B1 | . | 0 |
B1 | . | 0 |
B1 | . | 0 |
B1 | . | 0 |
B1 | . | 0 |
Regards,
Daniel
data have ;
id = 'A1' ; have = . ; output ;
id = 'A1' ; have = 100 ; output ;
id = 'A1' ; have = . ; output ;
id = 'B1' ; have = . ; output ;
id = 'B1' ; have = . ; output ;
id = 'B1' ; have = . ; output ;
run ;
proc sql ;
create table want as
select have.*,
case when id in
(select distinct id
from have
where have is not missing) then 1 else 0 end as want
from have ;
quit ;
Hi, here's a data step solution ...
data want;
want = 0;
do until (last.id);
set have (in=one) have;
by id;
if one and have then want = 1;
if ^one then output;
end;
run;
It's based on Howard Schreier's idea of a "self-interleave" ...
Interleaving a Dataset with Itself: How and Why
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.