data:
| ID | a | b |
| 1 | 10 | 2 |
| 1 | 200 | 14.4 |
| 1 | 400 | 5 |
| 1 | 600 | 5 |
want:
| ID | a | b | c (flag) |
| 1 | 10 | 2 | 0 |
| 1 | 200 | 14.4 | 0 |
| 1 | 400 | 5 | 0 |
| 1 | 600 | 5 | 1 |
So, variable b has same value 5 in rows 3 and 4. I need to flag first value (row 3)as 0 and subseqent rows containing value 5 as 1.
Thank you.
How about this then:
proc sort data=have;
by id b;
run;
data want;
set have;
by id b;
c = 0;
if not first.b and b=5 then c=1;
run;
- Jan
This is my 2 cents. Depending on what you'd like to do with variable a your final version can differ. But the use of first. will be my recommendation.
proc sort data=have;
by id b;
run;
data want;
set have;
by id b;
if first.b then c=0;
else c=1;
run;
Regards,
- Jan.
You code works but I need to flag only one numeric value 5. For instance, if there are duplicate 8 values , there is no need to flag
How about this then:
proc sort data=have;
by id b;
run;
data want;
set have;
by id b;
c = 0;
if not first.b and b=5 then c=1;
run;
- Jan
Thanks. It works ![]()
Good to hear. Glad to be of help. Please do not hesitate to flag the question as resolved.
Regards Jan.
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!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.