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

Hello everyone,

 

Thanks in advance for your help!

 

I have a data like this:

 

X1     X2      X3    ...

1       ...        ...

1       ...        ...

2       ...        ...

2       ...        ...

2       ...        ...

3       ...        ...

4       ...        ...

4       ...        ...

 

and I want to create a new variable (called Y) with binary values, which makes the data as such:

 

X1     X2      X3    ...  Y

1       ...        ...          0 

1       ...        ...          1

2       ...        ...          0

2       ...        ...          1

2       ...        ...          1

3       ...        ...          0

4       ...        ...          0

4       ...        ...          1

 

In other words, it puts 0 for a unique value of X1, 0 for the first non-unique value, and 1 for the remainder of non-unique values. I was wondering how it can be done in SAS. Any idea/help is really appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

Assuming data is sorted by X1 then do:

data want;
 set have;
  by X1;
       if first.x1  then y=0; else y=1;
run;

View solution in original post

4 REPLIES 4
Shmuel
Garnet | Level 18

Assuming data is sorted by X1 then do:

data want;
 set have;
  by X1;
       if first.x1  then y=0; else y=1;
run;
Alireza_Boloori
Fluorite | Level 6
@Shmuel Thank you for the answer!
Reeza
Super User

Similar method, slightly different calculation, since that's exactly what FIRST does, flip the indicator.

 

data want;
 set have;
  by X1;

 y=1-first.x1;
run;

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 1019 views
  • 0 likes
  • 3 in conversation