Desktop productivity for business analysts and programmers

Cleaning data inliers using CASE statement for multiple variables

Reply
New Contributor
Posts: 3

Cleaning data inliers using CASE statement for multiple variables

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.

Super User
Posts: 19,160

Re: Cleaning data inliers using CASE statement for multiple variables

Drop some of the quotes, and a case statement is usually formatted as below:

CASE

WHEN Gender = 'M' THEN  .

ELSE AgeMenst

END as AgeMenst

New Contributor
Posts: 3

Re: Cleaning data inliers using CASE statement for multiple variables

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.

enterprise guide issue.png

Community Manager
Posts: 2,889

Re: Cleaning data inliers using CASE statement for multiple variables

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

New Contributor
Posts: 3

Re: Cleaning data inliers using CASE statement for multiple variables

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?

PROC Star
Posts: 7,436

Re: Cleaning data inliers using CASE statement for multiple 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?

Super User
Posts: 19,160

Re: Cleaning data inliers using CASE statement for multiple variables

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

Contributor
Posts: 25

Re: Cleaning data inliers using CASE statement for multiple variables

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.

Super User
Posts: 19,160

Re: Cleaning data inliers using CASE statement for multiple variables

Yup, thanks Smiley Happy

Ask a Question
Discussion stats
  • 8 replies
  • 507 views
  • 4 likes
  • 5 in conversation