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?
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.
@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.
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.
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.
Ready to level-up your skills? Choose your own adventure.