SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.

Hello Experts,

 

I apply this function to get 1.50% or 1,50% :

if prxmatch("/\b1,5|\b1\.5/",lb_lg)>0 

But when I apply :

 if prxmatch("/\b1/",lb_lg)>0 

to get just 1% I have also 1,50%, 1.50% and 1%.

Which function do I need to apply to get only the data with 1% ?

 

Thank you for your help !

1 ACCEPTED SOLUTION

Accepted Solutions
quickbluefish
Barite | Level 11

It depends what you expect to come after the "1" in 1%. Do you expect it to read "1.0"? "1.0%", "1%", "1 %"?


Right now, all you're specifying in the 2nd prxmatch is that there's a "1" at the beginning of a word boundary, so that includes " 1", " 1.5", etc.


You could try, for example:

data test;
infile cards dsd truncover dlm=',';
length x $20;
input x;
cards;
this is 1.5%
this is 1 %
this is 1.0%
this is 1%
this is 2%
;
run;

data test;
set test;
length is_1pct 3;
is_1pct=0;
if prxmatch("/\b1(\.0\b|\.0\%|\%|\s)/",x)>0 then is_1pct=1;
run;

proc print data=test; run;

...but I would definitely do some testing - others here definitely know regex better than me.

View solution in original post

5 REPLIES 5
quickbluefish
Barite | Level 11

It depends what you expect to come after the "1" in 1%. Do you expect it to read "1.0"? "1.0%", "1%", "1 %"?


Right now, all you're specifying in the 2nd prxmatch is that there's a "1" at the beginning of a word boundary, so that includes " 1", " 1.5", etc.


You could try, for example:

data test;
infile cards dsd truncover dlm=',';
length x $20;
input x;
cards;
this is 1.5%
this is 1 %
this is 1.0%
this is 1%
this is 2%
;
run;

data test;
set test;
length is_1pct 3;
is_1pct=0;
if prxmatch("/\b1(\.0\b|\.0\%|\%|\s)/",x)>0 then is_1pct=1;
run;

proc print data=test; run;

...but I would definitely do some testing - others here definitely know regex better than me.

maguiremq
SAS Super FREQ

Thanks for the sample, @quickbluefish.

 

Is there a reason we need to use regex? I use regexes a lot, but I always forget how to use them until I need them.

 

data test;
infile cards dsd truncover dlm=',';
length x $20;
input x;
cards;
this is 1.5%
this is 1 %
this is 1.0%
this is 1%
this is 2%
this is 400   %
1.2% is wow
;
run;

data want;
	set test;
	/* Removing spaces */
	var = compress(x, , "kdp");
	/* keeping spaces but loses repeated spaces */
	var2 = compress(x, , "kdps");
run;

proc print; run; 

COMPRESS can do it, but it obviously starts to lose unique patterns. Can a string have multiple percentages? Does it have to have a percent sign to extract it? It's tough to get the right answer given all the possible scenarios.

x 	var 	var2
this is 1.5% 	1.5% 	1.5%
this is 1 % 	1% 	1 %
this is 1.0% 	1.0% 	1.0%
this is 1% 	1% 	1%
this is 2% 	2% 	2%
this is 400 % 	400% 	400 %
1.2% is wow 	1.2% 	1.2%

 

 

quickbluefish
Barite | Level 11

@maguiremq - yes, almost certainly could be done without regex, though the more complex / messy it gets, the more difficult it's going to be without it.  I don't use regex enough to use it fluently or in any of the really sophisticated ways, and frankly, if I need to do something complicated with regex, I'm more likely to use Python than SAS.  One thing about regex is that it's a very good use case for LLMs like ChatGPT - both for generating syntax or explaining / troubleshooting syntax that you already have. 

 

For this case, your compress approach seems good - I never really remember the various flags that go in the 3rd argument.  Overall, though, we'd really need to see the full scope of possible ways these percentages could appear in the data in order to give a complete answer.  Thanks for your response!

 

maguiremq
SAS Super FREQ
Apologies @quickbluefish - I wasn't questioning you. I was just asking general questions about the programming situation and accidentally replied to yours!

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
  • 5 replies
  • 663 views
  • 3 likes
  • 3 in conversation