BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
animesh123
Obsidian | Level 7

String = A Math B Cook V Deep .

Is there a way to get the output ABV using Compress function.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

No. 

 

That is NOT what COMPRESS() does.  It removes individual characters. So to get that result using COMPRESS() you would have to remove any character that wasn't A,B or V. Which would work for that one example, but I doubt it would work for other strings you might have.

 

If the pattern is single letter followed by single word then use SCAN() function in a loop.

data test;
   String = 'A Math B Cook V Deep';
   length next_word new_string $10 ;
   do i=1 by 2 until(next_word=' ');
     next_word=scan(string,i,' ');
     new_string=cats(new_string,next_word);
   end;
   drop i next_word;
run;

If the pattern is more complex 'D Earth Science E Chemistry' then you will need something more complicated.

 

If the goal is to find single uppercase letters then then perhaps something like:

data test;
   String = 'A Math B Cook V Deep';
   length next_word new_string $10 ;
   do i=1 to countw(string,' ');
     next_word=scan(string,i,' ');
     if length(next_word)=1 and 'A' <= next_word <= 'Z' then
       new_string=cats(new_string,next_word)
     ;
   end;
   drop i next_word;
run;

If not then you will probably need to use regular expressions instead.  

 

So if the pattern is remove any word that is more than one character then try

data test;
   String = 'A Math B Cook V Deep';
   length new_string $10 ;
   new_string=compress(prxchange('s/[^ ]{2,}//',-1,string),' ');
run;

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

No. 

 

That is NOT what COMPRESS() does.  It removes individual characters. So to get that result using COMPRESS() you would have to remove any character that wasn't A,B or V. Which would work for that one example, but I doubt it would work for other strings you might have.

 

If the pattern is single letter followed by single word then use SCAN() function in a loop.

data test;
   String = 'A Math B Cook V Deep';
   length next_word new_string $10 ;
   do i=1 by 2 until(next_word=' ');
     next_word=scan(string,i,' ');
     new_string=cats(new_string,next_word);
   end;
   drop i next_word;
run;

If the pattern is more complex 'D Earth Science E Chemistry' then you will need something more complicated.

 

If the goal is to find single uppercase letters then then perhaps something like:

data test;
   String = 'A Math B Cook V Deep';
   length next_word new_string $10 ;
   do i=1 to countw(string,' ');
     next_word=scan(string,i,' ');
     if length(next_word)=1 and 'A' <= next_word <= 'Z' then
       new_string=cats(new_string,next_word)
     ;
   end;
   drop i next_word;
run;

If not then you will probably need to use regular expressions instead.  

 

So if the pattern is remove any word that is more than one character then try

data test;
   String = 'A Math B Cook V Deep';
   length new_string $10 ;
   new_string=compress(prxchange('s/[^ ]{2,}//',-1,string),' ');
run;

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

From SAS Users blog
Want more? Visit our blog for more articles like these.
5 Steps to Your First Analytics Project Using SAS

For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 227 views
  • 0 likes
  • 3 in conversation