Joined: 28 Aug 2007 Posts: 1742 Location: Tirupur, India
Hi,
There is a SAS macro variable text1 with the value,
this is a text with ' , ) , % and )
I am trying to find the position of the first occurernce of ) on the SAS macro variable using index function.
Tried the below code, but unable to make the %index or %qindex to work,
could you please let me know how to correct the statements.
CODE:
Code:
option symbolgen mprint;
%let text1 = this is a text with %str(%') , ) , % and );
%put &text1;
%let text2 = %qindex("%str(&text1)","%str(%))");
%put &text2;
LOG:
Code:
1 option symbolgen mprint;
2 %let text1 = this is a text with %str(%') , ) , % and );
3 %put &text1;
SYMBOLGEN: Macro variable TEXT1 resolves to this is a text with ' , ) , %
and )
SYMBOLGEN: Some characters in the above value which were subject to macro
quoting have been unquoted for printing.
this is a text with ' , ) , % and )
4 %let text2 = %qindex("%str(&text1)","%str(%))");
WARNING: Apparent invocation of macro QINDEX not resolved.
SYMBOLGEN: Macro variable TEXT1 resolves to this is a text with ' , ) , %
and )
SYMBOLGEN: Some characters in the above value which were subject to macro
quoting have been unquoted for printing.
5 %put &text2;
SYMBOLGEN: Macro variable TEXT2 resolves to %qindex("this is a text with '
, ) , % and )",")")
SYMBOLGEN: Some characters in the above value which were subject to macro
quoting have been unquoted for printing.
WARNING: Apparent invocation of macro QINDEX not resolved.
%qindex("this is a text with ' , ) , % and )",")")
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
What is %QINDEX -- it is not listed as a SAS macro function in anything I can find?
This worked for me:
Code:
%LET TEXT1 = THIS IS A TEXT WITH %STR(%') , ) , % AND );
%PUT &TEXT1;
%LET TEXT3 = %STR(%)) ;
%PUT &TEXT3;
%LET TEXT2 = %INDEX(%UNQUOTE("%NRSTR(%BQUOTE(&TEXT1))"),&TEXT3);
%PUT &TEXT2;
Note the %INDEX function does NOT have quotes around the second parameter. The result returned for TEXT2 is 26 -- which is the first position of the parenthesis, counting the space after the equal sign of the %LET TEXT1= statement, and resolving the %STR function.
Joined: 28 Aug 2007 Posts: 1742 Location: Tirupur, India
Hello Robert,
Thanks for your thoughts.
Quote:
What is %QINDEX -- it is not listed as a SAS macro function in anything I can find?
It was my error. Functions like SUBSTR,SCAN,UPCASE have QSUBSTR,QSCAN,QUPCASE pair so I used QINDEX without looking it up.
Realised now that Q prefix is only for functions which return a character.
This also worked now,
Code:
option symbolgen mprint;
%let text1 = this is a text with %str(%') , ) , % and );
%put &text1;
%let text2 = %index("%str(&text1)",%STR(%)));
%put &text2;