BookmarkSubscribeRSS Feed
geneva
Calcite | Level 5

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.

8 REPLIES 8
Reeza
Super User

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

CASE

WHEN Gender = 'M' THEN  .

ELSE AgeMenst

END as AgeMenst

geneva
Calcite | Level 5

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

ChrisHemedinger
Community Manager

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

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
geneva
Calcite | Level 5

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?

art297
Opal | Level 21

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?

Reeza
Super User

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

DaveBirch
Obsidian | Level 7

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.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2433 views
  • 4 likes
  • 5 in conversation