BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
paddyb
Quartz | Level 8
 
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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

7 REPLIES 7
paddyb
Quartz | Level 8
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

kiranv_
Rhodochrosite | Level 12

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.

Reeza
Super User

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

Astounding
PROC Star

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;

paddyb
Quartz | Level 8

Thanks ,It worked

Tom
Super User Tom
Super User

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".

paddyb
Quartz | Level 8

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.

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
  • 7 replies
  • 1884 views
  • 0 likes
  • 5 in conversation