Hello all.
I'm getting started with SAS and I need a conditional variable starting from a non-numerical variable. My dataset looks like this:
CITY $ var2 var3
London . .
London . .
New York . .
Madrid . .
I'm using the IF THEN command in order to get the system to create a new numerical variable starting from the city names (IF CITY=London THEN CITYNR=1; IF CITY=Madrid THEN CITYNR=2; etc), but I can't bring the code to work. The output table has new columns for CITYNR, LONDON, MADRID, etc, which is of course something I don't want. Just for the sake of it I tried the same command starting from a numerical variable like var2 or var3 and it works.
What am I getting wrong? Can anyone help me? Thank you so much!
The logical test
IF CITY=LONDON
tells SAS to compare the CITY var to the LONDON var, so SAS assumes there is an (unitialized) var named LONDON.
You really want to test CITY var against a literal VALUE, as in
if CITY='LONDON' then .... ;
else if CITY='MADRID' then ....;
The logical test
IF CITY=LONDON
tells SAS to compare the CITY var to the LONDON var, so SAS assumes there is an (unitialized) var named LONDON.
You really want to test CITY var against a literal VALUE, as in
if CITY='LONDON' then .... ;
else if CITY='MADRID' then ....;
Ok, so I just tried putting the city names between apostrophes and it does work because I'm finally getting the CITYNR column I was looking for with the correct numbers, but I'm also still getting empty columns for each city name. What else am I getting wrong?
Post the code you are using.
Post your test data as a datastep, follow this post if you don't know how to:
Its very hard for us to be guessing everything, for instance, do you have spaces before or after the text? "ABC " is not the same as "ABC". Also show your code.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.