BookmarkSubscribeRSS Feed
SanjayM
Calcite | Level 5
I have a macro that has one parameter.

1. How can I validate for characters and numbers only

2. Macro fails when passing a value with single quotes

Please help
5 REPLIES 5
chang_y_chung_hotmail_com
Obsidian | Level 7
Well, I don't think this is an ODS or reporting issue at all, but ... You can use functions like verify or prxmatch to check the %superq()'ed argument, like:



   %macro isAlphaNumeric(str);


     %local r;


     %let r=%sysfunc(prxmatch(/[^A-Za-z0-9]/,%superq(str)));


     %eval(&r = 0)


   %mend  isAlphaNumeric;


 


   %put %isAlphaNumeric(ABC23);


   %put %isAlphaNumeric(AB!C);


   %put %isAlphaNumeric(%str(%'));


   %*-- on log


   1


   0


   0


   --*;





This still cannot prevent someone making a macro call like:



   %isAlphaNumeric(')





But as a macro writer, there is nothing you can do to prevent such stupidity.
SanjayM
Calcite | Level 5
Hello Chang,

The code fails as expects a double quotes "/[^A-Za-z0-9]/"

After applying the double quotes
%let r=%sysfunc(prxmatch("/[^A-Za-z0-9]/",%superq(str)));

The output is
1
1
1

Regards
Sanjay
polingjw
Quartz | Level 8
Chang’s solution worked perfectly when I ran the code.

On a side note, I naively thought that I could create a simpler solution that avoided Perl regular expressions by using the compress function with the a and d modifiers. Once I started working on this problem, however, my solution quickly became more complicated than Chang’s, plus it still doesn’t work correctly. I’m hoping that someone can help me understand what exactly went wrong in my solution.

I attempted to create a macro variable called ALPHA_NUMERIC that would resolve to either Yes or No depending on the value of the macro variable STR. Here is the assignment statement I tried to use:
[pre]
%let alpha_numeric = %sysfunc(ifc(%nrbquote(%sysfunc(compress(%superq(str), , ad))) = , Yes, No));

[/pre]
It actually works for the first two examples from Chang’s posting:
[pre]
1 %let str=ABC123;
2 %let alpha_numeric = %sysfunc(ifc(%nrbquote(%sysfunc(compress(%superq(str), , ad))) = , Yes,
2 ! No));
3 %put str=%superq(str) alpha_numeric=&alpha_numeric;
str=ABC123 alpha_numeric=Yes
4
5 %let str=AB!C;
6 %let alpha_numeric = %sysfunc(ifc(%nrbquote(%sysfunc(compress(%superq(str), , ad))) = , Yes,
6 ! No));
7 %put str=%superq(str) alpha_numeric=&alpha_numeric;
str=AB!C alpha_numeric=No

[/pre]
However, it does not work for the third example:
[pre]
8 %let str=%str(%');
9 %let alpha_numeric = %sysfunc(ifc(%nrbquote(%sysfunc(compress(%superq(str), , ad))) = , Yes,
9 ! No));
ERROR: Argument 1 to function IFC referenced by the %SYSFUNC or %QSYSFUNC macro function is not a
number.
ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list. Execution
of %SYSCALL statement or %SYSFUNC or %QSYSFUNC function reference is terminated.
10 %put str=%superq(str) alpha_numeric=&alpha_numeric;
str=' alpha_numeric=

[/pre]
I really don’t understand the first error message. It seems to me that the condition in the ifc function would clearly be false. I would have thought that the condition would be analogous to %eval(%str(%')= ), which SAS evaluates as 0.

I know that this error can easily be remedied by using macro %if %then logic or data step logic in conjunction with call symput. I would just like to know, for my own understanding, exactly why the above code fails.

Thanks.
chang_y_chung_hotmail_com
Obsidian | Level 7
@SanjayM: Don't be embarrassed or get discouraged by your mistakes. Keep on learning macro. You'll eventually get it. 🙂



@polingjw: It looks to me that it is a bug in the ifn function. As you've mentioned, a solution seems to be simply separating out the evaluation of the condition part.



   %let str=%str(%');


   %let cond = %sysevalf(%qsysfunc(compress(&str,,ad))=);


   %put %sysfunc(ifc(&cond,y,n));


   %*-- on log


     n


   --*;

polingjw
Quartz | Level 8
Thanks Chang. I’m somewhat relieved to hear that it looks like a bug to you to. I thought that I was probably overlooking something really obvious.

On another side note, how do you add all the colors to the code in your forum postings?

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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