Hello
I would like to mark value changes in a column like below:
| ID | VALUE | CHANGE |
| 1 | 10 | 0 |
| 1 | 10 | 0 |
| 1 | 12 | 1 |
| 1 | 12 | 0 |
| 1 | 15 | 1 |
In this example ID and VALUE is my data and I want to make CHANGE column equal 1 each time the value change in given ID
I've tried this code but it aplies 0 each row. Maybe lag function could be helpful but I'm not sure how to use it
data want;
set have;
by id value notsorted;
count=first.value;
if first.id then count=0;
run;
Can you help me with solution?
ID VALUE change 1 10 0 1 10 0 1 12 1 1 12 0 1 15 1
Works for me:
data have;
input ID VALUE;
datalines;
1 10
1 10
1 12
1 12
1 15
;
data want;
set have;
by id value notsorted;
change = first.value;
if first.id then change = 0;
run;
proc print data=want noobs;
run;
Result:
ID VALUE change 1 10 0 1 10 0 1 12 1 1 12 0 1 15 1
ID VALUE change 1 10 0 1 10 0 1 12 1 1 12 0 1 15 1
Works for me:
data have;
input ID VALUE;
datalines;
1 10
1 10
1 12
1 12
1 15
;
data want;
set have;
by id value notsorted;
change = first.value;
if first.id then change = 0;
run;
proc print data=want noobs;
run;
Result:
ID VALUE change 1 10 0 1 10 0 1 12 1 1 12 0 1 15 1
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 lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.