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

Hello Experts,

 

Today, I'm using the following code to extract rates. I'm wondering if I can create a macro using PRXMATCH to check the values,  from 0.50% to 2.70% ?

 

if (prxmatch("/\b0[,\.]45\s*%/",lb_lg)>0
or prxmatch("/\b0[,\.]45\s*%/",lb_)>0)
or (prxmatch("/\b0[,\.]45\s*%/",lb_lg)>0
or prxmatch("/\b0[,\.]45\s*%/",lb)>0) then
do;
Taux=0.0045;
end;

if (prxmatch("/\b0[,\.]50\s*%/",lb_lg)>0
or prxmatch("/\b0[,\.]50\s*%/",lb)>0)
or (prxmatch("/\b0[,\.]5\s*%/",lb_lg)>0
or prxmatch("/\b0[,\.]5\s*%/",lb)>0) then
do;
Taux=0.005;
end;

if prxmatch("/\b1(\.0\b|\.0\%|\%|\s)/",lb_lg)>0 
or prxmatch("/\b1(\.0\b|\.0\%|\%|\s)/",lb)>0 then
do;
Taux=0.01;
end;

Thank you !

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Note that since you are using two different variables as your sources, Lb_lg and Lb your code example quite likely is not returning what you would want because you would require if/then/else structures. consider if lb_lg has a value of .45 and Lb is .5 (given the appropriate other string characters). Do you want Taux to be 0.0045 or 0.005? Without an ELSE between the first and second IF/then the second comparison will reset Taux to 0.005 after it had been set to 0.0045 in the first block.

 

I would also suggest using these rates as numeric and investigate the SELECT / WHEN comparison structure to get a clearer code for the hierarchy with two apparently similarly ranged variables. Of course if there was only a single variable involved an custom format likely could be used to remove the logic completely out of the data step.

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

I think far easier is to convert the text which you describe as 0.50% to 2.70% to actual numbers and then checking becomes much much much much easier. And then likely no macro is needed.

--
Paige Miller
SASdevAnneMarie
Rhodochrosite | Level 12

Thank you, Paige Miller, but I didn't fully understand your idea.

Tom
Super User Tom
Super User

Use the INPUT() function to convert the string into the number it represents and then it will be much easier to compare.

 

For more help show some examples of the input strings you need to test.

SASdevAnneMarie
Rhodochrosite | Level 12
Thank you, Tom.
I'm wondering if I can do this with "prxmatch" :
%macro mymacro(var);
if (prxmatch("/\b0[,\.]&VAR.\s*%/",lb_lg)>0 then ....
%mend;
%mymacro(var=50);
PaigeMiller
Diamond | Level 26

@SASdevAnneMarie wrote:
Thank you, Tom.
I'm wondering if I can do this with "prxmatch" :
%macro mymacro(var);
if (prxmatch("/\b0[,\.]&VAR.\s*%/",lb_lg)>0 then ....
%mend;
%mymacro(var=50);

Your are wondering if you can do this with prxmatch. The answer is for you to try it. 


it is impossible for me to advise you further since I don't know what you are trying to do. Would you please explain what you are trying to do? Please explain in words without reference to PRXMATCH or any other code.

 

--
Paige Miller
PaigeMiller
Diamond | Level 26

@SASdevAnneMarie wrote:

Thank you, Paige Miller, but I didn't fully understand your idea.


The whole idea is that numbers are easier to handle as numbers than if you leave them as text. If there was something I could advise less experienced people to do, it would be to ALWAYS convert text strings which are really numbers to actual numbers. 

--
Paige Miller
ballardw
Super User

Note that since you are using two different variables as your sources, Lb_lg and Lb your code example quite likely is not returning what you would want because you would require if/then/else structures. consider if lb_lg has a value of .45 and Lb is .5 (given the appropriate other string characters). Do you want Taux to be 0.0045 or 0.005? Without an ELSE between the first and second IF/then the second comparison will reset Taux to 0.005 after it had been set to 0.0045 in the first block.

 

I would also suggest using these rates as numeric and investigate the SELECT / WHEN comparison structure to get a clearer code for the hierarchy with two apparently similarly ranged variables. Of course if there was only a single variable involved an custom format likely could be used to remove the logic completely out of the data step.

SASdevAnneMarie
Rhodochrosite | Level 12
Thank you, Ballardw!

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand in the Innovate Hub.

Watch 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
  • 8 replies
  • 385 views
  • 7 likes
  • 4 in conversation