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

Dear all,

 

how to replace 'AND' (if COUNTRY_CODE='GB'), 'UND'(if COUNTRY_CODE='GE'), 'E'(if COUNTRY_CODE='IT') as '&'?

 

especially, replaced 'AND' should be a single word(i.e., it is 'a and b', rather than 'aandb' )

 

could you please give me some suggestion about this?

thanks in advance.

 

data a;
  infile datalines dlm=',';
  input COUNTRY_CODE $4. name &25;
datalines;
GB, apple and juicande
GB, apple und juicand
GE, plus und flundy
IT, jjeed e dinedd
run;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

You should first make sure that your example code creates the values you want us to manipulate.

Perhaps something like:

data a;
  infile datalines dlm=',' truncover;
  informat COUNTRY_CODE $4. name $25.;
  input COUNTRY_CODE name  4-25 ;
datalines;
GB, apple and juicande
GB, apple und juicand
GE, plus und flundy
IT, jjeed e dinedd
run;

Your example has country_code ending with , and the name blank.

 

 

Second please show exactly what you want for output. Perhaps:

data a;
  infile datalines dlm=',' truncover;
  informat COUNTRY_CODE $4. name $25.;
  input COUNTRY_CODE name  4-25 ;
  if  COUNTRY_CODE='GB' then name=tranwrd(name,' and ',' & ');
  if  COUNTRY_CODE='GE' then name=tranwrd(name,' und ',' & ');
datalines;
GB, apple and juicande
GB, apple und juicand
GE, plus und flundy
IT, jjeed e dinedd
run;

Including the leading and trailing spaces for the words and or und and then including them in the replacement string seems to work.

 

View solution in original post

1 REPLY 1
ballardw
Super User

You should first make sure that your example code creates the values you want us to manipulate.

Perhaps something like:

data a;
  infile datalines dlm=',' truncover;
  informat COUNTRY_CODE $4. name $25.;
  input COUNTRY_CODE name  4-25 ;
datalines;
GB, apple and juicande
GB, apple und juicand
GE, plus und flundy
IT, jjeed e dinedd
run;

Your example has country_code ending with , and the name blank.

 

 

Second please show exactly what you want for output. Perhaps:

data a;
  infile datalines dlm=',' truncover;
  informat COUNTRY_CODE $4. name $25.;
  input COUNTRY_CODE name  4-25 ;
  if  COUNTRY_CODE='GB' then name=tranwrd(name,' and ',' & ');
  if  COUNTRY_CODE='GE' then name=tranwrd(name,' und ',' & ');
datalines;
GB, apple and juicande
GB, apple und juicand
GE, plus und flundy
IT, jjeed e dinedd
run;

Including the leading and trailing spaces for the words and or und and then including them in the replacement string seems to work.

 

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
  • 1 reply
  • 627 views
  • 1 like
  • 2 in conversation