Hi guys! I am doing one sdtm project question,
there is one variable "folder" to keep the visiting week values like "week1" "screening" "week5"; also, we want to create a new variable "visitnum" to extract the numbers of folder, like if folder=week5, we keep 5 for the visitnum;
when the folder="SRV", we want to keep the previous value, like for observation80, we have folder="SRV", so we keep the previous obs(79) folder="week7", and we extract 7 for "visitnum";
I am trying to use RETAIN function to keep the previous value, but still get error.
Can you help me to fix it? Thank!
General rule on this forum when asking about errors or requesting suggestions for code: Copy the text from the log including the code and any notes, messages, warnings or errors and then on the forum open a text box using the </> icon that appears above the message window. Paste the text.
The text box means that we can copy text and edit it to make suggestions. The LOG means that we have all the text of the errors and any diagnostic information that SAS often supplies with errors or warnings and know more of what happens. Most of us are unwilling to retype from scratch large amounts of code to make small change to a program.
You have at least one logic error: you never assign a value to Prev_folder.
I suspect you may have intended to have a :
PREV_FOLDER = lag(Folder);
immediately before the "If first.vetest" to actually get the value of the previous folder.
Second, since you use Retain that way the variable Prev_folder has been defined as numeric by default. If you want it to hold character values then you want to assign the value as Character before the Retain with something like:
Length Prev_folder $ 15;
Or look up the syntax for Retain to set a default value of Prev_folder that can be used to set the length.
the number needs to be large enough to hold the longest expected value of Folder.
Hi ballardw! Thanks for your replay, I add PREV_FOLDER = lag(Folder) and it worked!
I used to think that we can only use lag for numerical variables.
Besides abovementioned comments, please note that LAG function works as long as your data structure is 'one record per visit'. If your data is already in SDTM structure which assumes (FINDINGS DOMAINS) to be one record per --TEST, per visit, then you might need to use RETAIN statement to perform LOCF properly.
Below is an example of lag function (prev_folder_lag) and retain statements(prev_folder_retain):
data have;
input id $ vstestcd $ folder $;
cards;
100011 PULSE week1
100011 TEMP week1
100011 WEIGHT week1
100011 PULSE week2
100011 TEMP week2
100011 WEIGHT week2
100011 PULSE SRV
100011 TEMP SRV
100011 WEIGHT SRV
;
data want;
set have;
prev_folder_lag= lag(folder);
length prev_folder_retain $14;
retain prev_folder_retain;
if upcase(folder) ne 'SRV' then prev_folder_retain= folder;
run;
Yes, LAG function can be applied to both numeric and character variables.
But the related DIF function can only be used with numeric variables (although SAS will try convert numeric text to numeric values for use by DIF).
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.