BookmarkSubscribeRSS Feed
chennupriya
Quartz | Level 8

hi I hav a dataset which has both numericals and character . When numeric ala I want to change to character but when character leave as it is . When 1 I want to be replaced with FL when it is AZ leave as it is in STate variable

ssample dataset

ddata test1;

input code zipcode;

1 78240

2 23239

3 45670

AL 34567

AZ 43678

;

run;

proc sql;

create te table xy as

select code ,

pincode,

case code

when 1 then 'FL'

when 2 then 'CA'

when 3 then 'SC'

else code

end as State;

from test1;

quit;

but but this syntax is error

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, the reason is in your sample dataset, SAS automatically assigns the first column to be a character variable as you have non-numeric data in it.  Hence later on when you try to do a case on numeric data then you get an error as the variable is character.  Just change the case to look at characters (also correct some typos in your code):

proc sql;

     create table xy as

     select      code,

                    pincode,

                    case code      when "1" then 'FL'

                                          when "2" then 'CA'

                                          when "3" then 'SC'

                                         else code end as State

    from test1;

quit;

chennupriya
Quartz | Level 8

Hi

iit worked thank you Smiley Happy

Reeza
Super User

Please mark the question answered.

ballardw
Super User

You may also find the function ZIPSTATE useful. If your Zip code is accurate then State=zipstate(zipcode).

You could just use: select zipstate(zipcode) as state

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 895 views
  • 2 likes
  • 4 in conversation