BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ppchennn
Fluorite | Level 6

I was learning about the RETAIN statement and came across a paper about it (RETAIN or NOT? Is LAG Far Behind? by Venky Chakravarthy).

 

The example is about using the retain statement to replace missing value with substituted data (substitute with the last observation).

 

I was confused with a line in the example data step.

 

The example is like this:

DATA SYSTOLIC2;
INPUT PTNO SBP;
CARDS;
1 120
1 .
1 140
2 150
2 130
2 .
3 135
3 .
3 .
3 .
;
RUN;
Data impute2 ;
set systolic2 ;
by ptno ;
retain newsbp ;
if first.ptno then newsbp = . ; *I was confused by this line.;
if not missing(sbp) then newsbp = sbp ;
run ;

The data step creates a new variable called newsbp to replace the missing value in the sbp variable with the last observation.


I tried to delete the line that confuses me and the output and log was the same with the example.


Do we really need to set the first newsbp of a ptno missing to make the data step work?

 

Since all the sbp of first.ptno is not missing, what is the purpose of setting the first newsbp missing?

 

 

It would be great if anyone could help, thanks! 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

It is useless in this specific example but,as you say, if there was a ptno with first observation missing,
it would avoid to report the spb from the previous ptno.

View solution in original post

2 REPLIES 2
gamotte
Rhodochrosite | Level 12

It is useless in this specific example but,as you say, if there was a ptno with first observation missing,
it would avoid to report the spb from the previous ptno.

ppchennn
Fluorite | Level 6

At first, I thought the by ptno line is enough for avoiding the problem of getting the observation from the previous ptno.
I tried to change the example sbp data and got the idea of what you mean.

 

Thanks a lot for the help!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1557 views
  • 1 like
  • 2 in conversation