SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAShole
Pyrite | Level 9

It looks like SAS has built in function to convert from State FIPS to full state name and abbreviation to full state name but I can't find one to go from full state name to abbreviation.

data have;
     input state $50.;
cards;
Alabama
Alaska
Arizona
;;;;
run;

 Edit: Want:

State Abbreviation
Alabama Al
Alaska AK
Arizona AZ
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Then you just need to add another function call to the code to generate the date for the format.

Since you are now mapping character to character you can use either a format and the PUT() function or a character informat and the INPUT() function.

 

data fips;
  fmtname='$FIPS';
  length fips 8 label $2 start $20 ;
  do fips=1 to 95;
    start=fipnamel(fips);
    if start ne 'Invalid Code' then do;
       label=fipstate(fips);
       output;
    end;
  end;
run;

proc format cntlin=fips ; run;

data test;
 set fips;
 statecode=put(start,$fips.);
run;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

There is a function to convert the code into a name like that.

So use that to make an informat you can use to convert the name to fips code.

data fips;
  fmtname='FIPS';
  type='I';
  length label 8 start $20 ;
  do label=1 to 95;
    start=fipnamel(label);
    if start ne 'Invalid Code' then output;
  end;
run;

proc format cntlin=fips ; run;

Let's use the FIPS dataset we just made  to test the new informat.

data test;
 set fips;
 fipscode=input(start,fips.);
run;

Results:

Obs    fmtname    type    label    start                   fipscode

  1     FIPS       I         1     Alabama                     1
  2     FIPS       I         2     Alaska                      2
  3     FIPS       I         4     Arizona                     4
  4     FIPS       I         5     Arkansas                    5
  5     FIPS       I         6     California                  6
  6     FIPS       I         8     Colorado                    8
  7     FIPS       I         9     Connecticut                 9
  8     FIPS       I        10     Delaware                   10
  9     FIPS       I        11     District of Columbia       11
 10     FIPS       I        12     Florida                    12
...
SAShole
Pyrite | Level 9
I'm actually looking to convert to the abbreviation. Apologies, as I think I confused the question by mentioned FIPS code.
Tom
Super User Tom
Super User

Then you just need to add another function call to the code to generate the date for the format.

Since you are now mapping character to character you can use either a format and the PUT() function or a character informat and the INPUT() function.

 

data fips;
  fmtname='$FIPS';
  length fips 8 label $2 start $20 ;
  do fips=1 to 95;
    start=fipnamel(fips);
    if start ne 'Invalid Code' then do;
       label=fipstate(fips);
       output;
    end;
  end;
run;

proc format cntlin=fips ; run;

data test;
 set fips;
 statecode=put(start,$fips.);
run;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 3 replies
  • 5326 views
  • 2 likes
  • 2 in conversation