BookmarkSubscribeRSS Feed
Sikcion
Fluorite | Level 6

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!

 

Screenshot 2023-12-11 at 3.59.04 AM.png

6 REPLIES 6
JosvanderVelden
SAS Super FREQ
Please post the saslog from the start with code, warnings and errors.
ballardw
Super User

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.

Sikcion
Fluorite | Level 6

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.

A_Kh
Lapis Lazuli | Level 10

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; 

 

mkeintz
PROC Star

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

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Astounding
PROC Star
A couple of issues stand out but there are probably many more. So you will need to clean these up before (as others have rightfully suggested) posting the log.

First, your program refers to first.vetest. But nothing in your program creates first.vetest. Where does it come from?

And second, where does prev_folder come from? What does it contain?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 825 views
  • 0 likes
  • 6 in conversation