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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
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

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User
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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

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
  • 1 reply
  • 559 views
  • 0 likes
  • 2 in conversation