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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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