SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
india2016
Pyrite | Level 9

Hi All,

Need small help. I have data like below

AUnique_IDVersion
Murder11
Theft12
dacoity13
Murder21
Murder22

 

I want an output like below:

 

Unique IDPrevious_APresent_A
1TheftDacoity
2MurderMurder

 

Basically if for Unique_ID there is a version. For the last version and before last version if Column "A" is different then I need 2 column to be created Previous_A and Present_A. Previous A will hold the 2nd last version value and Present A will hold the last version value.

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data have;
input A $ Unique_ID Version;
datalines;
Murder 1 1
Theft 1 2
dacoity 1 3
Murder 2 1
Murder 2 2
;

data want;
   set have; 
   by Unique_ID Version; 
   lagA=lag1(A);
   if last.Unique_ID then do;
      Previous_A=lagA;
      Present_A=A;
      output;
   end;
   keep Unique_ID Previous_A Present_A;
run;

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20
data have;
input A $ Unique_ID Version;
datalines;
Murder 1 1
Theft 1 2
dacoity 1 3
Murder 2 1
Murder 2 2
;

data want;
   set have; 
   by Unique_ID Version; 
   lagA=lag1(A);
   if last.Unique_ID then do;
      Previous_A=lagA;
      Present_A=A;
      output;
   end;
   keep Unique_ID Previous_A Present_A;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 794 views
  • 0 likes
  • 2 in conversation