BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Phil_NZ
Barite | Level 11

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.

 

 
Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

5 REPLIES 5
Shmuel
Garnet | Level 18

Assuming currency is always 3 characters then:

len = length(counytry);
short_fn = substr(country,1,len-3);
currency = substr(country,len-2,3);
Phil_NZ
Barite | Level 11

Hi @Shmuel 

I really appreciate your answer. Just wondering if we should use function "cats" here?

 
Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
Shmuel
Garnet | Level 18

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.

ChrisNZ
Tourmaline | Level 20

> 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?

Tom
Super User Tom
Super User

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
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
  • 5 replies
  • 1430 views
  • 4 likes
  • 4 in conversation