SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
blueskyxyz
Lapis Lazuli | Level 10
data test1;
	length string $1000;
    string= "THIS IS A REALLY LONG STRING THAT I WANT TO SPLIT G LONG ENOUGH TO SPLIT";
	array str{4} $ 50 ('not', 'is', 'a', 'to' ) ;
	mid=string;
	do i=1 to 4;
		y=trim(upcase(str{i}));
		x=trim(str{i});
		if index(mid,y) then do; mid=tranwrd(mid,y ,x );
			put x y mid;
		end;
	end;
	string=mid;
run;

my code cannot work, to get the final result as below:

THIS is a REALLY LONG STRING THAT I WANT to SPLIT G LONG ENOUGH to SPLIT

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data test1;
	length string want $1000;
    string= "THIS IS A REALLY LONG STRING THAT I WANT TO SPLIT G LONG ENOUGH TO SPLIT";
	array str{4} $ 50 ('not', 'is', 'a', 'to' ) ;
	call missing(want);
	do i=1 to countw(string,' ');
      temp=scan(string,i,' ');
	  if lowcase(temp) in str then want=catx(' ',want,lowcase(temp));
	   else want=catx(' ',want,temp);
	end;

run;

View solution in original post

11 REPLIES 11
blueskyxyz
Lapis Lazuli | Level 10

replace a word a time, this can work, for example, tranwrd(string,"IS" ,"is" );

but it cannot work using array and do-loop, why? how to modify the code?

 

D_ph14
Calcite | Level 5

Try using trim for variables x and y:

 

if index(mid,trim(y)) then do;
  mid=tranwrd(mid, trim(y), trim(x) );
  put x y mid;
end;

blueskyxyz
Lapis Lazuli | Level 10
Thanks @D_ph14 , but it can get what I want
Ksharp
Super User
data test1;
	length string want $1000;
    string= "THIS IS A REALLY LONG STRING THAT I WANT TO SPLIT G LONG ENOUGH TO SPLIT";
	array str{4} $ 50 ('not', 'is', 'a', 'to' ) ;
	call missing(want);
	do i=1 to countw(string,' ');
      temp=scan(string,i,' ');
	  if lowcase(temp) in str then want=catx(' ',want,lowcase(temp));
	   else want=catx(' ',want,temp);
	end;

run;
blueskyxyz
Lapis Lazuli | Level 10
@Ksharp ,I appreciate your help, if the variable has a lot of characters, it will time consuming to combine each word a time using catx function.
Ksharp
Super User
Did you try Patrick's PRXCHANGE() ?
ed_sas_member
Meteorite | Level 14

Hi @blueskyxyz 

 

You can easily do that using the PRXCHANGE function:

 

data test1;
	length string $1000;
	string="THIS IS A REALLY LONG STRING THAT I WANT TO SPLIT G LONG ENOUGH TO SPLIT";
	mid=prxchange('s/(\bNOT\b|\bIS\b|\bA\b|\bTO\b)/\L$1/', -1, string);
run;

This code :

1 - Search for the pattern NOT or IS or A or TO (\b identifies a word boundary)

2 - Replace it ($1) by its lowercase equivalent (lowcase  = \L) as many times as needed (-1 as a second argument)

Patrick
Opal | Level 21

Same as @ed_sas_member just with a small change to the RegEx

data _null_ ;                                                                                                                  
  s = "IS IT POSSIBLE TO LOWCASE WORDS A IS NOT TO IN A LONG STRING USING TRANWRD OR NOT" ;  
  s=prxchange('s/\b(a|is|not|to)\b/\L\1/oi',-1,trim(s));                                                                                                      
  put s ;  
  stop; 
run; 
blueskyxyz
Lapis Lazuli | Level 10
Hi @ed_sas_member, thanks for your method using RegEx.

It is very clear with your detailed explanations.
hashman
Ammonite | Level 13

@blueskyxyz:

Possible. But you need to:

  • make sure that only the words in the list are lowcased if surrounded by blanks 
  • account for the endpoints with one-sided blanks if any

Here's one way:

data want (keep = s) ;                                                                                                                  
  s = "IS IT POSSIBLE TO LOWCASE WORDS A IS NOT TO IN A LONG STRING USING TRANWRD OR NOT" ;                                             
  array w $3 w1-w4 ("a" "is" "not" "to") ;                                                                                              
  retain b "" ;                                                                                                                         
  do over w ;                                                                                                                           
    s = left (tranwrd (b || s || b, b || upcase (trim (w)) || b, b || trim (w) || b)) ;                                                 
  end ;                                                                                                                                 
  put s ;                                                                                                                              
run ;     

Result:

is IT POSSIBLE to LOWCASE WORDS a is not to IN a LONG STRING USING TRANWRD OR not

Kind regards

Paul D.

blueskyxyz
Lapis Lazuli | Level 10
Thanks @hashman , my confusion is clear up.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 11 replies
  • 2765 views
  • 4 likes
  • 6 in conversation