BookmarkSubscribeRSS Feed
vickys
Obsidian | Level 7

I have a long string like this

"fruits: mangovegetables: carrotdrinks: cokeseafood: fishrestaurant: jasmineplace: London"

 

But I am trying to add "." (dot) so that I can differentiate which category they are and the expected string is as below

"fruits: mango.vegetables: carrot.drinks: coke.seafood: fish.restaurant: jasmine.place: London"

 

I tried like something

 

data want;

set have;

_list = o_list;

length word $200 ;

do

word='vegetables:', 'drinks:', 'seafood:', 'restaurant:', 'place:' ;

n_list = tranwrd(n_list, strip(word), .word);

end;

n_list = compbl(n_list);

drop word ;

run;

but they are doing nothing to the string. same string with out transwrd.

Appreciate your help Guru's

 

Thanks in advance

2 REPLIES 2
ed_sas_member
Meteorite | Level 14

Hi @vickys 

 

Please try this:

data have;
	length n_list $ 200;
	n_list="fruits: mangovegetables: carrotdrinks: cokeseafood: fishrestaurant: jasmineplace: London";
run;

data want;
	set have;
	array word (5) $12 _temporary_ ('vegetables:', 'drinks:', 'seafood:', 'restaurant:', 'place:');
	do i=1 to dim(word);
		n_list = prxchange(cats('s/(.*)(',word(i),')(.*)/$1.$2$3/'),1,n_list);
	end;
	drop i;
run;

Best,

 

 

ballardw
Super User

One thing that you want to consider is that you are adding characters to a string. So the result should be longer that what you start with. Depending on the defined length of a variable you may get truncation because the longer result does not fit into the original variable.

So you either need to create a new variable or do something to address the length issue. Here's an example:

 

data example;
   n_list="fruits: mangovegetables: carrotdrinks: cokeseafood: fishrestaurant: jasmineplace: London";
   do word='vegetables:', 'drinks:', 'seafood:', 'restaurant:', 'place:' ;
      n_list = tranwrd(n_list, strip(word), cats('.',strip(word)));
      put n_list=;
   end;
run;

Look in the log and you will see the result start losing characters at the end.

Since you have 5 words that you are replacing with an added character you need to have a result variable that is at least 5 characters longer than your source variable or define the variable to be longer.

data example;
   length n_list $ 100;
   n_list="fruits: mangovegetables: carrotdrinks: cokeseafood: fishrestaurant: jasmineplace: London";
   do word='vegetables:', 'drinks:', 'seafood:', 'restaurant:', 'place:' ;
      n_list = tranwrd(n_list, strip(word), cats('.',strip(word)));
      put n_list=;
   end;
run;

Also you should always read your log. Your program had an error because of the way you called tranwrd:

28      n_list = tranwrd(n_list, strip(word), .word);
                                                ---
                                                22
                 -------
                 72
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, ',', -, /, <, <=,
              <>, =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=,
              |, ||, ~=.

ERROR 72-185: The TRANWRD function call has too many arguments.

If you want to add a character you need to use a function or operator to do so. Using .word is neither.  The use of dot plus a letter or an underscore references a special missing value in SAS. So the .w is treated as a numeric missing generating the "Numeric values have been converted …" warning. Then the characters following the ORD of word are treated as another variable which is undefined so also treated as numeric. Both of those mean too many arguments for Tranwrd.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1525 views
  • 1 like
  • 3 in conversation