Hi SAS Users,
Previously, I want to generate a new variable short_fn the first three characters of a country name (fname) with an underline "_", so my code is
short_fn= cats(substr(fname, 1,3),'_');
Therefore, when fname=Argentina_ => short_fn =ARG_
fname=UK_ => short_fn=UK__
Now, I want to change the code to be compatible of the new name
For example, now the fname are ArgentinaARS, United KingdomGBP, and United StatesUSD
And I want to write code to get two separate variables:
short_fn: ARGENTINA, UNITED KINGDOM, UNITED STATES
currency: ARS, GBP, USD
Can you please help me to sort it out? (If we can make use of the function cats or substr, it would be great, if not, I am also willing to learn new SAS function)
Many thanks in advance and warmest regards.
It is not clear what you are asking for. It sounds like you have input like:
data have ;
input fname $32. ;
cards;
ArgentinaARS
United KingdomGBP
United StatesUSD
;
So you will want to write code like this:
data want;
set have;
length short_fn $29 currency $3 ;
short_fn = substr(fname,1,length(fname)-3);
currency = substr(fname,length(fname)-2);
run;
Results:
Obs fname short_fn currency 1 ArgentinaARS Argentina ARS 2 United KingdomGBP United Kingdom GBP 3 United StatesUSD United States USD
Assuming currency is always 3 characters then:
len = length(counytry);
short_fn = substr(country,1,len-3);
currency = substr(country,len-2,3);
Hi @Shmuel
I really appreciate your answer. Just wondering if we should use function "cats" here?
1) Function CATS is one out of few functions CATx - where x can be space or S or T or X - all of them are variations of CONCATENATNG strings.
2) Other functions dealing with strings helps to deal with substrings like: SUBSTR, FINDW, INDEX, INDEXC, TRANWRD, TRANSLATE and others.
You can learn a lot by reading SAS Documentation and run examples given within the documentation.
Easy way to find the documentation is by searching Google:
"sas documentation " followed by any sas keyword: function name, procedure name etc.
> Just wondering if we should use function "cats" here?
@Shmuel answers the question as you described it. There is nothing to concatenate that I can see. Is there?
It is not clear what you are asking for. It sounds like you have input like:
data have ;
input fname $32. ;
cards;
ArgentinaARS
United KingdomGBP
United StatesUSD
;
So you will want to write code like this:
data want;
set have;
length short_fn $29 currency $3 ;
short_fn = substr(fname,1,length(fname)-3);
currency = substr(fname,length(fname)-2);
run;
Results:
Obs fname short_fn currency 1 ArgentinaARS Argentina ARS 2 United KingdomGBP United Kingdom GBP 3 United StatesUSD United States USD
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.