🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 10-12-2018 02:00 AM
(793 views)
Hi All,
Need small help. I have data like below
A | Unique_ID | Version |
Murder | 1 | 1 |
Theft | 1 | 2 |
dacoity | 1 | 3 |
Murder | 2 | 1 |
Murder | 2 | 2 |
I want an output like below:
Unique ID | Previous_A | Present_A |
1 | Theft | Dacoity |
2 | Murder | Murder |
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks @PeterClemmensen.