BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasvader
Fluorite | Level 6

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:

IDHaveWant
A1.1
A1.1
A11001
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

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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;

View solution in original post

8 REPLIES 8
SteveDenham
Jade | Level 19

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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 Smiley Happy

SteveDenham
Jade | Level 19

Ah, but my wager was that there WAS a simple way to do this in PROC SQL.  I too accept PayPal...

Steve Denham

Haikuo
Onyx | Level 15

Sorry, Steve, You are already rich enough Smiley Happy

proc sql;

create table want as

select *, sum(not missing(have))>0 as want

from have

group by id;

quit;

Ksharp
Super User

Code: Program

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;
Haikuo
Onyx | Level 15

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;

hbi
Quartz | Level 8 hbi
Quartz | Level 8

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;

MikeZdeb
Rhodochrosite | Level 12

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 8 replies
  • 4091 views
  • 8 likes
  • 7 in conversation