Help using Base SAS procedures

Retain Last obs to new variable

Reply
Regular Contributor
Posts: 229

Retain Last obs to new variable

data l;
input id;
cards;
45
63
1023
run;

Now i want to retain the last obs to new variable;

output:
id new_id
45 1023
63 1023
1023 1023
Here 1023 is the last obs it shold move to new variable new_id
Super Contributor
Super Contributor
Posts: 365

Re: Retain Last obs to new variable

Hello R_Win;

This is a solution:
[pre]
data l;
input id;
cards;
45
63
1023
run;
data _null_;
set l end=l;
if l then call symputx ('id',id);
run;
data r;
set l;
new_id=&id;
run;
[/pre]
Sincerely,
SPR
Super Contributor
Posts: 578

Re: Retain Last obs to new variable

just for clarification....

set l end=l;

That's the letter L...not the number 1. Sometimes confusing in certain fonts.
Super User
Posts: 10,046

Re: Retain Last obs to new variable

[pre]
data l;
input id;
cards;
45
63
1023
;
run;

data want;
set l nobs=nobs;
if _n_ eq 1 then set l(rename=(id=new_id)) point=nobs;
run;
[/pre]

Ksharp
Ask a Question
Discussion stats
  • 3 replies
  • 124 views
  • 0 likes
  • 4 in conversation