Just learning SAS EG, and this has me completely stumped. I would like to recode a variable dependent on the value of a different variable for the same row. The example I am working with is this: Males do not menstruate, so I would want to exclude the age of menstruation for Males. I attempted to create a recoded column marking the age of menstruation as missing for males using a CASE statement, but my new column had 1s and 0s instead of the original value. The advanced expression was:
CASE
WHEN 'Gender' = 'M' THEN 'AgeMenst' = .
ELSE 'AgeMenst' = 'AgeMenst'
END
What am I doing wrong? I have tried multiple different combinations. I've tried leaving out the ELSE statement.
Drop some of the quotes, and a case statement is usually formatted as below:
CASE
WHEN Gender = 'M' THEN .
ELSE AgeMenst
END as AgeMenst
This is what I get when using that format (the variable names are actually 'MnstAge Recoded' and 'Gender Recoded'). Any ideas? Have also tried with 'M' instead of M.
This looks like a code generation error with non-standard variables names, and I'm pretty sure that it has been addressed in a SAS Enterprise Guide hotifx. What version of SAS Enterprise Guide are you running? Check to make sure that you have the latest fixes.
Chris
I'm using 4.3. I apologize, because I'm pretty new to this--is there a fix for this version, or would I be better off renaming my variables?
Are those actually your variable names? SAS variable names can't contain spaces or special characters unless you have the system option validvarname set to any, which isn't recommended.
Is age already calculated and, if so, what is the variable name?
Then try leaving the quote but making the other changes, and storing it in a new variable.
CASE
WHEN 'Gender Recoded' = 'M' THEN .
ELSE 'MnstAge Recoded'
END as New_Age_Menst
Reeza's code still needs a little adjustment:
CASE
WHEN 'Gender Recoded'n = 'M' THEN .
ELSE 'MnstAge Recoded'n
END as New_Age_Menst
As Arthur noted, SAS variable names don't normally contain spaces or special characters. If you want to refer to variables with a space in the name you need to surround the name with quote marks and follow it with the n modifier.
Yup, thanks
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.