BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PremS
Obsidian | Level 7

I've got this utility macro that I commonly use to add prefixes/suffixes to a list of terms. Recently I've also been using this to shorthand macro variable resolution, but despite the use of macro quoting, I'm getting a resolution warning of "Apparent symbolic reference N not resolved".

 

From my testing, the place where the warning appears is in the %QSYSFUNC(PRXCHANGE()) function.

Below is the macro and a simple example illustrating the warning (I'm running SAS 9.4M3). I've included an example where if the prefix N had a value, it doesn't get resolved anyway. Almost like the macro compiler is doing a check for the macro variable but isn't actually trying to resolve it (not sure on why it would do this).

 

This nonissue has been bothering me for some time, appreciate any thoughts!

 

 

%macro qmodify_list(string, dlm_charlist=%str( ,), add_prefix=, add_suffix=, replace_dlm=) ;
%*----------------------------------------------------------------------------*;
%* STRING - list of delimited words                                           *;
%* DLM_CHARLIST - delimiter for words in STRING                               *;
%*                                                                            *;
%* ADD_PREFIX - characters to add before each word in STRING                  *;
%* ADD_SUFFIX - characters to add after each word in STRING                   *;
%*  * NOTE: Above can use substitution regex characters, like $1 to add the   *;
%*          the matched string as a prefix or suffix                          *;
%*                                                                            *;
%* REPLACE_DLM - characters to replace all consecutive delimiters             *;
%*                                                                            *;
%* Common applications:                                                       *;
%*                                                                            *;
%*  - Append prefix for a RENAME= option:                                     *;
%*       %modify_list(var1 var2 var3, add_prefix=%str($1=new_))               *;
%*         -> returns: var1=new_var1 var2=new_var2 var3=new_var3              *;
%*                                                                            *;
%*  - Get a comma separated list of quoted values for an IN operator          *;
%*       %modify_list(PPN1 PPN2, add_prefix=%bquote(")                        *;
%*                              , add_suffix=%bquote(")                       *;
%*                              , replace_dlm=%str(,)                         *;
%*                     )                                                      *;
%*         -> returns: "PPN1","PPN2"                                          *;
%*                                                                            *;
%* Returned text is macro quoted, primarily used for input to other macros    *;
%*----------------------------------------------------------------------------*;

%if %superq(string) = %then %do;
   %put ERROR: [dev] List to modify (first parameter) was not provided.;
   %put ERROR: [dev] Macro will ABORT CANCEL.;
   %abort cancel;
%end;
%else %if %superq(add_prefix) =  and %superq(add_suffix) = and %superq(replace_dlm) =  %then %do;
   %put WARNING: [dev] Neither ADD_PREFIX=, ADD_SUFFIX=, nor replace_dlm= parameters were given.;
   %put WARNING: [dev] Supply at least one parameter to utilize this macro.;
   %put WARNING: [dev] Original list will be returned.;
   %superq(string);
%end;
%else %do;
   %* BE SURE TO ESCAPE the charaters in the ADD_PREFIX or ADD_SUFFIX parameters with \
      TO MASK PRX METACHARACTERS! E.g. \$ for $ or \\ for \;

   %if %superq(replace_dlm) ne %then %do;
      %let string = %qsysfunc(prxchange(s~[%superq(dlm_charlist)]+~%superq(replace_dlm)~,-1,%superq(string)));
      %let dlm_charlist = %superq(replace_dlm);
   %end;

   %qsysfunc(prxchange(s!([^%superq(dlm_charlist)]+)!%superq(add_prefix)$1%superq(add_suffix)!,-1,%superq(string)))

%end;

%mend qmodify_list;

%macro modify_list(string, dlm_charlist=%str( ,), add_prefix=, add_suffix=, replace_dlm=);
   %unquote(%qmodify_list(%superq(string)
                        , dlm_charlist=%superq(dlm_charlist)
                        , add_prefix=%superq(add_prefix)
                        , add_suffix=%superq(add_suffix)
                        , replace_dlm=%superq(replace_dlm)
                          )
            )
%mend;


%let n1 = A;
%let n2 = B;

%put %qmodify_list(1 2, add_prefix=%nrstr(&n));
%put %modify_list(1 2, add_prefix=%nrstr(&n));

%let m = whoops;
%let m1 = C;
%let m2 = D;

%put %qmodify_list(1 2, add_prefix=%nrstr(&m));
%put %modify_list(1 2, add_prefix=%nrstr(&m));
98         %put %qmodify_list(1 2, add_prefix=%nrstr(&n));
WARNING: Apparent symbolic reference N not resolved.
&n1 &n2
99         %put %modify_list(1 2, add_prefix=%nrstr(&n));
WARNING: Apparent symbolic reference N not resolved.
A B
100        
101        %let m = whoops;
102        %let m1 = C;
103        %let m2 = D;
104        
105        %put %qmodify_list(1 2, add_prefix=%nrstr(&m));
&m1 &m2
106        %put %modify_list(1 2, add_prefix=%nrstr(&m));
C D

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Why not use %QSCAN() and TRANWRD() instead?

%macro qmodify_list(string, dlm_charlist=%str( ,), add_prefix=, add_suffix=, replace_dlm=) ;
%local i word sep;
%let add_prefix=%qsysfunc(tranwrd(&add_prefix,$1,%nrstr(&word)));
%let add_suffix=%qsysfunc(tranwrd(&add_suffix,$1,%nrstr(&word)));
%do i=1 %to %sysfunc(countw(&string,&dlm_charlist));
  %let word=%qscan(&string,&i,&dlm_charlist);
  %*;%bquote(&sep.%unquote(&add_prefix.)&word.%unquote(&add_suffix.))%*;
  %let sep=&replace_dlm;
%end;
%mend qmodify_list;

Test:

144   %put %qmodify_list(var1 var2 var3, add_prefix=$1=new_,replace_dlm=%str( ));
var1=new_var1 var2=new_var2 var3=new_var3

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Where is &n created or defined?

--
Paige Miller
Tom
Super User Tom
Super User

Sounds like the issue is how %SYSFUNC()/%QSYSFUNC() is trying to evaluate the arguments to your function call. 

In general I have found that %SYSFUNC() does not play that well with others. It seems to be related to functions that can have arguments that can either be strings or numbers.  It seems like %SYSFUNC() is trying to figure out whether the text (everything in macro code is text) being passed is a number or a string.

 

Can you recreate the issue without the rest of your macro code to make debugging easier?

 

PremS
Obsidian | Level 7

@PaigeMiller The N isn't a macro variable that is meant to be defined. N1 and N2 are two defined macro variables, and (using macro quoting) I was trying to assemble the text &N1 &N2. The warning is coming up even though the %NRSTR(&N) is quoted when called with SUPERQ.

 

Below is a simpler version of the code to assist with debugging. The same warning is still produced:

 

%macro qmodify_list(string, dlm_charlist=%str( ,), add_prefix=, add_suffix=, replace_dlm=) ;
   %qsysfunc(prxchange(s!([^%superq(dlm_charlist)]+)!%superq(add_prefix)$1%superq(add_suffix)!,-1,%superq(string)))
%mend qmodify_list;

%macro modify_list(string, dlm_charlist=%str( ,), add_prefix=, add_suffix=, replace_dlm=);
   %sysfunc(prxchange(s!([^%superq(dlm_charlist)]+)!%superq(add_prefix)$1%superq(add_suffix)!,-1,%superq(string)))
%mend;

* Self-contained test runs ;
%let n1 = A;
%let n2 = B;

%put %qmodify_list(1 2, add_prefix=%nrstr(&n));
%put %modify_list(1 2, add_prefix=%nrstr(&n));

%let m = whoops;
%let m1 = C;
%let m2 = D;

%put %qmodify_list(1 2, add_prefix=%nrstr(&m));
%put %modify_list(1 2, add_prefix=%nrstr(&m));
* Self-contained test runs ;
39         %let n1 = A;
40         %let n2 = B;
41         
42         %put %qmodify_list(1 2, add_prefix=%nrstr(&n));
WARNING: Apparent symbolic reference N not resolved.
&n1 &n2
43         %put %modify_list(1 2, add_prefix=%nrstr(&n));
WARNING: Apparent symbolic reference N not resolved.
A B
44         
45         %let m = whoops;
46         %let m1 = C;
47         %let m2 = D;
48         
49         %put %qmodify_list(1 2, add_prefix=%nrstr(&m));
&m1 &m2
50         %put %modify_list(1 2, add_prefix=%nrstr(&m));
C D
PaigeMiller
Diamond | Level 26

If &N is not defined, then you get the error. SAS is looking for &N and it doesn't exist.

 

I don't understand your explanation "The N isn't a macro variable that is meant to be defined."

--
Paige Miller
Quentin
Super User

Agree with @Tom  .  In theory, you shouldn't need those %superq since you've quoted the value with %nrstr() before passing it into the macro.  I think something in the innerworkings of PRXCHANGE is unquoting the value, and causing the problem.

 

This trivial example with UPCASE works:

%macro qmodify_list(add_prefix=) ;
   %qsysfunc(upcase(&add_prefix))
%mend qmodify_list;

%put %qmodify_list(add_prefix=%nrstr(&n));

Change to CATS and it tries to resolve macro variable n:

%macro qmodify_list(add_prefix=) ;
   %qsysfunc(CATS(&add_prefix))
%mend qmodify_list;

%put %qmodify_list(add_prefix=%nrstr(&n));

CATS is also one of those pesky functions that can accept numeric or character values, and I think it unquotes values in the process, unfortunately.

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Tom
Super User Tom
Super User

Why not use %QSCAN() and TRANWRD() instead?

%macro qmodify_list(string, dlm_charlist=%str( ,), add_prefix=, add_suffix=, replace_dlm=) ;
%local i word sep;
%let add_prefix=%qsysfunc(tranwrd(&add_prefix,$1,%nrstr(&word)));
%let add_suffix=%qsysfunc(tranwrd(&add_suffix,$1,%nrstr(&word)));
%do i=1 %to %sysfunc(countw(&string,&dlm_charlist));
  %let word=%qscan(&string,&i,&dlm_charlist);
  %*;%bquote(&sep.%unquote(&add_prefix.)&word.%unquote(&add_suffix.))%*;
  %let sep=&replace_dlm;
%end;
%mend qmodify_list;

Test:

144   %put %qmodify_list(var1 var2 var3, add_prefix=$1=new_,replace_dlm=%str( ));
var1=new_var1 var2=new_var2 var3=new_var3
PremS
Obsidian | Level 7

@Quentin @Tom Yeah I could believe Tom is right about how the character processing works for certain functions.

 

The QSCAN/TRANWRD does circumvent the warning, thanks for the suggestion! I had to reorder the TRANWRD to happen after the text to prevent warning messages due to unquoting ADD_PREFIX.

 

%macro qmodify_list(string, dlm_charlist=%str( ,), add_prefix=, add_suffix=, replace_dlm=) ;
   %local i word sep term ;

   %do i=1 %to %sysfunc(countw(&string,&dlm_charlist));
      %let word=%qscan(&string,&i,&dlm_charlist);
      %let term = %qsysfunc(tranwrd(%bquote(&sep.&add_prefix.&word.&add_suffix.), %str($1), %superq(word)));

      %*; &term %*;
      
      %let sep=&replace_dlm;
   %end;
%mend qmodify_list;

%let n1 = A;
%let n2 = B;
%put %qmodify_list(1 2, add_prefix=%nrstr(&n));
Tom
Super User Tom
Super User

Your macro is inserted un-requested spaces.

For example this call has empty value for replace_dlm but it is still inserting a space between the terms.

160   %put %qmodify_list(1 2, add_prefix=%nrstr(&n));
&n1 &n2

It is doing that because you added a space after the comment in front on where you expanded TERM.

Use code like this instead.

      %*;&term

If you want the default to have a space inserted then set that as the default value for the REPLACE_DLM parameter.

%macro qmodify_list(string,dlm_charlist=%str( ,),add_prefix=,add_suffix=,replace_dlm=%str( )) ;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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