The dataset I'm using has an individual variable for each phecode such as "X250.12, X250.13, X250.14...." which has "TRUE" or "FALSE" depending on whether each observation (patient) has any of these diagnoses.
I want to create a new variable called "Diabetes" that includes all the observations that say "true" for at least one of these variables (X250.12, X250.13, X250.14...). I know there is an easier way of doing this besides what I have attempted here but I can't remember for the life of me:
IF X250= "TRUE" THEN Diabetes= 1;
IF X250.1= "TRUE" THEN Diabetes= 1;
IF X250.11= "TRUE" THEN Diabetes=1;
Thank you!!
Like this?
DIABETES = index(catt(of X250:), 'TRUE')>0;
X250.1 is not a valid variable NAME.
You can have a character variable with a VALUE of 'X250.1'.
There are times when data supplied really needs to be cleaned up for use. I suspect this is one of the occasions.
First, if the variable names are actually X250.1 then you reference them incorrectly. To have any character other than letter, digit and the _ character in a SAS variable name you have to use "name literal" construction and would reference the variable that appears like that as "X250.1"n the quotes and the n immediately following tells SAS you intend to use that string as variable name.
Second, data with text "True" and "False" is awkward. It is better for a number of reasons to have numeric 1/0 coded values. SAS will use 1 as "True" and 0 as "False".
Then you could code such as
Diabetes = (max(list the variable names here separated by commas) = 1 );
SAS will return the result of a comparison as 1/0.
If you make the variable names standard SAS names such as X250_1 instead of X205.1 then the above would allow use of variable lists such as
Diabetes = ( max(of x250:)=1);
The colon after the X250 creates a variable list that would use all of the variable names starting with X250.
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.