DATA Step, Macro, Functions and more

Question about RETAIN statement.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Question about RETAIN statement.

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! Smiley Happy


Accepted Solutions
Solution
‎03-16-2017 05:09 AM
Regular Contributor
Posts: 233

Re: Question about RETAIN statement.

[ Edited ]

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


All Replies
Solution
‎03-16-2017 05:09 AM
Regular Contributor
Posts: 233

Re: Question about RETAIN statement.

[ Edited ]

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.

Occasional Contributor
Posts: 5

Re: Question about RETAIN statement.

[ Edited ]

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!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 212 views
  • 1 like
  • 2 in conversation