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!
Assuming data is sorted by X1 then do:
data want;
set have;
by X1;
if first.x1 then y=0; else y=1;
run;
Assuming data is sorted by X1 then do:
data want;
set have;
by X1;
if first.x1 then y=0; else y=1;
run;
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
Get started using SAS Studio to write, run and debug your SAS programs.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.