BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
cashaowan
Fluorite | Level 6

I am concatenating two separate character variables into one variable, specifically FIP state and FIP county variables, both character variables. I don't want any space between FIP state and county codes in the new FIPS code created for each county.

 

Can I use the following codes?

Option 1: using traditional trim function

FIPS length $5.;

FIPS=trim(FIPstate)||trim(FIPcnty); 

 

Option 2: using cats function

FIPs length $5.;

FIPS=cats(FIPstate,FIPcnty);

If there is no delimit adding to two digits FIPstate and three digits FIPcnty, can I use cat function this way to deliver the desired five-digit format FIPs code variable?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Do  your county codes have leading zeroes if the numeric value is less than 100? If not then you are creating a malformed FIPS code. 2 character FIPS is state, 5 character is State + County. If the "county" FIP code is 1 and the state code is 16 then the state+county is 16001. If your state is a single digit then it needs to have a leading 0.

If your state and county codes have the leading 0 and they are defined as 2 character and 3 character then any of CAT, CATS or CATT would yield a 5 digit code IF you set the length before assigning. Otherwise the function will default to $200.

 

data example;
   length Fipstate $ 2 Fipcnty $ 3;
   Fipstate='02';
   Fipcnty ='001';
   length F1 F2 F3 $ 5;
   F1 = cat(fipstate,fipcnty);
   F2 = cats(fipstate,fipcnty);
   F3 = catt(fipstate,fipcnty);

run;
   

If they don't have leading zeroes you have more work to do to make sure they are there.

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

@cashaowan wrote:

I am concatenating two separate character variables into one variable, specifically FIP state and FIP county variables, both character variables. I don't want any space between FIP state and county codes in the new FIPS code created for each county.

 

Can I use the following codes?


Best way to answer that question is to try it yourself and see what happens. SAS will give you a very quick answer, faster than you can get from this forum, and it will be 100% guaranteed correct (which isn't true if I try to answer SAS questions). You don't need our help here.

--
Paige Miller
ballardw
Super User

Do  your county codes have leading zeroes if the numeric value is less than 100? If not then you are creating a malformed FIPS code. 2 character FIPS is state, 5 character is State + County. If the "county" FIP code is 1 and the state code is 16 then the state+county is 16001. If your state is a single digit then it needs to have a leading 0.

If your state and county codes have the leading 0 and they are defined as 2 character and 3 character then any of CAT, CATS or CATT would yield a 5 digit code IF you set the length before assigning. Otherwise the function will default to $200.

 

data example;
   length Fipstate $ 2 Fipcnty $ 3;
   Fipstate='02';
   Fipcnty ='001';
   length F1 F2 F3 $ 5;
   F1 = cat(fipstate,fipcnty);
   F2 = cats(fipstate,fipcnty);
   F3 = catt(fipstate,fipcnty);

run;
   

If they don't have leading zeroes you have more work to do to make sure they are there.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2 replies
  • 743 views
  • 1 like
  • 3 in conversation