BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
axescot78
Quartz | Level 8

I have data by user ID that is receive on a monthly basis. There is one variable that can change, once or twice or more. I want to be create a flag for those individuals that have this variable changed and record that changes.

 

What I want:
UserID changed res1 res2 res3
1693      1               Y      M
1129      1                Y      N      M
1345      1               N     M      Y


What I have:

data fake_data;
input userID $ date date9. response $ ;
format date date9.;
datalines;
1693 01Dec2014 Y
1693 01Jan2015 Y
1693 01Feb2015 Y
1693 01Mar2015 M
1693 01Apr2015 M
1693 01May2015 M
1129 01Feb2018 Y
1129 01Mar2018 Y
1129 01Apr2018 N
1129 01May2018 N
1129 01Jun2018 M
1129 01Jul2018 M
1345 01Aug2016 N
1345 01Sep2016 N
1345 01Oct2016 N
1345 01Nov2016 N
1345 01Dec2016 M
1345 01Jan2017 M
1345 01Feb2017 Y
1345 01Mar2017 Y
1345 01Apr2017 Y
1345 01May2017 Y
;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

This should work. I have changed the arrangement of the output data set to a format that works better in SAS (in other words, most SAS procedures need a long data set to work properly, whereas you wanted to create a wide data set).

 

data want;
    set fake_data;
    by userID notsorted;
    prev_response=lag(response);
    if first.userID then output;
    else if response^=prev_response then output;
    drop prev_response date;
run;

 

--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26

This should work. I have changed the arrangement of the output data set to a format that works better in SAS (in other words, most SAS procedures need a long data set to work properly, whereas you wanted to create a wide data set).

 

data want;
    set fake_data;
    by userID notsorted;
    prev_response=lag(response);
    if first.userID then output;
    else if response^=prev_response then output;
    drop prev_response date;
run;

 

--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 457 views
  • 0 likes
  • 2 in conversation