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?
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.
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?
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.
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.
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;
Thanks ,It worked
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".
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.