DATA Step, Macro, Functions and more

Creating a conditional variable starting from a non-numerical variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Creating a conditional variable starting from a non-numerical variable

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!


Accepted Solutions
Solution
‎10-02-2017 10:20 AM
Trusted Advisor
Posts: 1,284

Re: Creating a conditional variable starting from a non-numerical variable

Posted in reply to LunaMinerva

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 ....;

 

 

View solution in original post


All Replies
Solution
‎10-02-2017 10:20 AM
Trusted Advisor
Posts: 1,284

Re: Creating a conditional variable starting from a non-numerical variable

Posted in reply to LunaMinerva

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 ....;

 

 

Occasional Contributor
Posts: 8

Re: Creating a conditional variable starting from a non-numerical variable

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?

Super Contributor
Super Contributor
Posts: 260

Re: Creating a conditional variable starting from a non-numerical variable

Posted in reply to LunaMinerva

Post the code you are using.

Super User
Super User
Posts: 9,200

Re: Creating a conditional variable starting from a non-numerical variable

Posted in reply to LunaMinerva

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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