BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I have a data which looks like this and i want to copy the last obesrvation of height to all the previous rows of a given subject

Sub Date Age Rank
1 xxx 23
1 xxy 20
1 xyy 13
1 xyx 16 1
2 xyx 12
2 xyy 14
2 xzz 15 2

I would like to copy the rank value of a given subject to all the previous dates (rows)
4 REPLIES 4
Doc_Duke
Rhodochrosite | Level 12
SAS doesn't really do a "read ahead", but it does readily do a "retain". If the data always have the rank at the end, you could sort by sub and descending rank and then use a data step and retain to propagate the values to the "new" forward.

Doc Muhlbaier
Duke
Peter_C
Rhodochrosite | Level 12
although SAS does not "read ahead", this is not forecasting so just read it any way you want 😉

For a base SAS table, using the POINT= option of the SET statement, you can "read in reverse".[pre]data updated_data ;
do _n_ = top to 1 by -1 ;
set your.data point= _n_ nobs= top ;
if not missing(rank) then old_rank= rank ;
else rank = old_rank ;
output ;
end ;
stop ;
drop old_rank ;
run;[/pre]

peterC
data_null__
Jade | Level 19
[pre]
data have;
infile cards missover;
input Sub Date$ Age Rank;
cards;
1 xxx 23
1 xxy 20
1 xyy 13
1 xyx 16 1
2 xyx 12
2 xyy 14
2 xzz 15 2
;;;;
run;
data need;
merge have(drop=rank) have(keep=sub rank where=(not missing(rank)));
by sub;
run;
proc print;
run;
[/pre]
Peter_C
Rhodochrosite | Level 12
good demo _null_
my backward solution pays no respect to subject

peterC

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 1260 views
  • 0 likes
  • 4 in conversation