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
Besides @xia keshan 's Merge, there are also many other data step approach, a classic 2XDOW would be one, a verbose Hash, or such as the following stacking approach:
data have;
infile cards expandtabs;
input ID $ Have;
cards;
A1 .
A1 .
A1 100
A1 .
A1 .
A1 .
A1 .
A1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
;
run;
data want;
set have(in=up) have(in=down);
by id;
retain want;
if first.id then want=0;
if not missing(have) then want=1;
if down then output;
run;
Without writing code, I'm not sure, but how about something like using PROC MEANS (or SUMMARY) to get the max value for 'Have" for each level of ID. Then create 'Want' in the output dataset (something like:
if maxval>. then want=1 else want=0;
and then finally merging back on the original dataset with ID as the key.
I will wager a nearly infinite sum of money that there is a PROC SQL approach that would do this as well.
Steve Denham
Hi,
proc sql;
create table WANT as
select A.*,
case when exists(select distinct ID from HAVE where ID=A.ID and HAVE is not null) then 1
else 0 end as WANT
from HAVE A;
quit;
I accept Paypal SteveDenham
Ah, but my wager was that there WAS a simple way to do this in PROC SQL. I too accept PayPal...
Steve Denham
Sorry, Steve, You are already rich enough
proc sql;
create table want as
select *, sum(not missing(have))>0 as want
from have
group by id;
quit;
data have;
infile cards expandtabs;
input ID $ Have;
cards;
A1 .
A1 .
A1 100
A1 .
A1 .
A1 .
A1 .
A1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
;
run;
data want;
merge have have(rename=(Have=H) where=(H is not missing) in=inb);
by ID;
want=inb;
drop H;
run;
Besides @xia keshan 's Merge, there are also many other data step approach, a classic 2XDOW would be one, a verbose Hash, or such as the following stacking approach:
data have;
infile cards expandtabs;
input ID $ Have;
cards;
A1 .
A1 .
A1 100
A1 .
A1 .
A1 .
A1 .
A1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
B1 .
;
run;
data want;
set have(in=up) have(in=down);
by id;
retain want;
if first.id then want=0;
if not missing(have) then want=1;
if down then output;
run;
This accomplishes the same using a SAS hash object. Enjoy!
data want;
set have;
if _n_ = 1 then do; *declare a SAS hash object;
declare hash t(dataset: 'have(where=(have is not missing))');
t.definekey('ID');
t.definedone();
end;
Want=0; *set initial value;
if T.find()=0 then Want=1; *if found, change Want to 1;
run;
Hi, just a small variation on an already posted 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
http://www.lexjansen.com/nesug/nesug03/cc/cc002.pdf
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.