The SAS Output Delivery System and reporting techniques

Single Quotes issue

Reply
Frequent Contributor
Posts: 97

Single Quotes issue

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
Regular Contributor
Posts: 241

Re: Single Quotes issue

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.
Frequent Contributor
Posts: 97

Re: Single Quotes issue

Posted in reply to chang_y_chung_hotmail_com
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
Regular Contributor
Posts: 171

Re: Single Quotes issue

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.
Regular Contributor
Posts: 241

Re: Single Quotes issue

@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


   --*;

Regular Contributor
Posts: 171

Re: Single Quotes issue

Posted in reply to chang_y_chung_hotmail_com
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?
Ask a Question
Discussion stats
  • 5 replies
  • 311 views
  • 0 likes
  • 3 in conversation