## Create dummy variable for group

Solved
Occasional Contributor
Posts: 9

# Create dummy variable for group

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

Accepted Solutions
Solution
‎08-28-2015 09:13 AM
Posts: 3,167

## Re: Create dummy variable for group

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;

All Replies
Posts: 2,655

## Re: Create dummy variable for group

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

Super User
Posts: 9,599

## Re: Create dummy variable for group

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

Posts: 2,655

## Re: Create dummy variable for group

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

Steve Denham

Posts: 3,167

## Re: Create dummy variable for group

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;

Super User
Posts: 10,761

## Re: Create dummy variable for group

### 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;`
Solution
‎08-28-2015 09:13 AM
Posts: 3,167

## Re: Create dummy variable for group

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;

Contributor
Posts: 66

## Re: Create dummy variable for group

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;

Valued Guide
Posts: 765

## Re: Create dummy variable for group

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

🔒 This topic is solved and locked.