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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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