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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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