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