DATA Step, Macro, Functions and more

open() function unquotes values?

Posts: 1,232

open() function unquotes values?

Hi All,

Short question: Why does the open() function apparently unquote macro variables.

For example:

340  %put %sysfunc(open  (in(where=(var="%nrstr(%Something)"))));

WARNING: Apparent invocation of macro SOMETHING not resolved.


The user has quoted the value because %Something is a text string, not a macro invocation, but the open function is unmasking it, producing the warning message. (The result is still fine).

Compare the above result to the length() function, which does NOT unmask the value (expected behavior).

341  %put %sysfunc(length(in(where=(var="%nrstr(%Something)"))));


In this simple case of course user could change the code to use single quotes to hide the percent sign:

  %put %sysfunc(open(in(where=(var='%Something')));

But wondering if people agree that it is odd that open() is unquoting, and if there is a way to prevent this.  It feels like a bug to me, does it to you?

Longer version/background:

This came up because an autocall library had a %nobs macro, something like:

%macro nobs(data);   /*macro function in autocall library to return # obs in a dataset*/

%local nobs dsid rc;

%let dsid=%sysfunc(open(&data));

%let nobs=%sysfunc(attrn(&dsid,NLOBSF));

%let rc=%sysfunc(close(&dsid));


%mend nobs;

User wrote a macro like:

%macro report(data=,type=);

%if %nobs(&data(where=(type="&type")))=0 %then %do;

  data _null_;  

  file print;  

  put "There were no records in &data with type=&type";



%else %do;

  title1 "Printout of &data where Type=&Type";

  proc print data=&data;

      where type="&type";




%mend report;

And got the warning message when they ran:

data test;

input Year Type $14. Value;


2001 AbsoluteChange 10

2002 AbsoluteChange 20

2003 AbsoluteChange 30

2001 %Change        5

2002 %Change        10

2003 %Change        15




So the user deduces that it is %nobs which is unmasking the percent sign in %Change, and calls the guy who maintains the autocall library and asks him to fix it.  But I don't see how %nobs can be fixed to avoid this problem.  Given the number of %nobs (and other) macro functions that are floating around autocall libraries that use the open function, was wondering if anyone had a good fix. If not, I guess I'm stuck adding a limitation to every macro that uses the open() function to say that it will unquote values, and telling the user to redesign %report.



Ask a Question
Discussion stats
  • 0 replies
  • 1 in conversation