BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
LunaMinerva
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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 hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

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 hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
LunaMinerva
Fluorite | Level 6

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?

HB
Barite | Level 11 HB
Barite | Level 11

Post the code you are using.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Post your test data as a datastep, follow this post if you don't know how to:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

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.

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1377 views
  • 1 like
  • 4 in conversation