DATA Step, Macro, Functions and more

Question about RETAIN statement.

Accepted Solution Solved
Reply
Contributor
Posts: 55
Accepted Solution

Question about RETAIN statement.

[ Edited ]
 
data se;
retain sestdtc seendtc; set exr; by usubjid etcd element dtc; if first.element then sestdtc = dtc; if last.element then seendtc = dtc; if last.element; run;

The above code not working.I need to keep seendtc same as sestdtc for next etcd as shown on right side of image.Any suggestions please?txt.png


Accepted Solutions
Solution
‎02-06-2018 12:19 AM
Super User
Posts: 23,724

Re: Question about RETAIN statement.

FIRST/LAST/RETAIN don't work in this situation because you're crossing variables.

 

Instead try the LAG() function to capture the previous value. You can use FIRST/LAST to reset the values as needed, but you didn't post enough data for that. 

 

Please try and make your images bigger in the future so they're easily legible, they're barely legible at this point. 

 

 

 

Screen Shot 2018-02-05 at 7.16.02 PM.png

View solution in original post


All Replies
Contributor
Posts: 55

Question about RETAIN statement.

data se;

retain sestdtc seendtc;
 set exr;
 by usubjid etcd element dtc;
 if first.element then sestdtc = dtc;
 if last.element then seendtc = dtc;
  if last.element;

 run;

The above code not working.I need to keep seendtc same as sestdtc for next etcd as shown on right side of image.Any suggestions please?txt.pngtxt.png

PROC Star
Posts: 509

Re: Question about RETAIN statement.

please give your want and output in form of datastep, that will be make bit easy for someone who wants to help you. you have just showed one scenario, it would be better, if you can show more than one scenario.

Solution
‎02-06-2018 12:19 AM
Super User
Posts: 23,724

Re: Question about RETAIN statement.

FIRST/LAST/RETAIN don't work in this situation because you're crossing variables.

 

Instead try the LAG() function to capture the previous value. You can use FIRST/LAST to reset the values as needed, but you didn't post enough data for that. 

 

Please try and make your images bigger in the future so they're easily legible, they're barely legible at this point. 

 

 

 

Screen Shot 2018-02-05 at 7.16.02 PM.png

Super User
Posts: 6,774

Re: Question about RETAIN statement.

Your program refers to variables that don't exist, such as ELEMENT and DTC.  Well, maybe they exist but we have no idea what is in them.  So let's take the results in the data set SE and fix them according to what you say that you want:

 

data want;

set se;

by usubjid;

prior_val = lag(seendtc);

if first.usubjid=0 then sestdtc = prior_val;

drop prior_val;

run;

Contributor
Posts: 55

Re: Question about RETAIN statement.

Posted in reply to Astounding

Thanks ,It worked

Super User
Super User
Posts: 8,111

Re: Question about RETAIN statement.

Photographs of your data is going to be really hard for someone to use in testing solutions for you.

What was your input data?
What is the output your want for that input data?

One thing to check if that the variable you are trying to "retain" is not already on the input dataset. Because if it is the SET statement will overwrite the value you are trying to "retain".

Contributor
Posts: 55

Re: Question about RETAIN statement.

sorry ,it was not actual dataset. I created dummy to ask query ,Actually I wanted to retain value of seendtc and assign as seendtc for next element.Thanks for looking into it.

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 216 views
  • 0 likes
  • 5 in conversation