DATA Step, Macro, Functions and more

Problem with macro variable resolution in dataset name

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Problem with macro variable resolution in dataset name

[ Edited ]

I am having a problem I believe involves the resolution of a macro variable in a dataset name. It appears a space is being inserted between the first part of the name and the macro variable reference value, so that, instead of 1 name, I end up with 2 names. Below is sample code, and the log. The code was submitted immediately after SAS invocation (v9.4). The purpose of the %put and put statements is to show what the macro variable is resolving to.

%macro datatype(type);
  %global nmend;
  %if &type=enrolled %then %do;
    %let nmend=%str(enrl);
  %end;
  %else %do;
    %let nmend=%str(elig);
  %end;
%mend datatype;

%macro table1_3;
%put ***&nmend.***;
%put ***tbl1_3_&nmend.***;
%put ***%str(tbl1_3_&nmend.)***;
  data tbl1_3_&nmend.;
    name='Dave';
    age=22;
put "&nmend.";
put "tbl1_3_&nmend.";
put "%str(tbl1_3_&nmend.)";
    output;
  run;
%mend table1_3;

%datatype(elig)
%put &=nmend;
%table1_3
MLOGIC(DATATYPE):  Beginning execution.
MLOGIC(DATATYPE):  Parameter TYPE has value elig
MLOGIC(DATATYPE):  %GLOBAL  NMEND
SYMBOLGEN:  Macro variable TYPE resolves to elig
MLOGIC(DATATYPE):  %IF condition &type=enrolled is FALSE
MLOGIC(DATATYPE):  %LET (variable name is NMEND)
MLOGIC(DATATYPE):  Ending execution.
54   %put &=nmend;
SYMBOLGEN:  Macro variable NMEND resolves to elig
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted for printing.
NMEND=elig
55   %table1_3
MLOGIC(TABLE1_3):  Beginning execution.
MLOGIC(TABLE1_3):  %PUT ***&nmend.***
SYMBOLGEN:  Macro variable NMEND resolves to elig
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted for printing.
***elig***
MLOGIC(TABLE1_3):  %PUT ***tbl1_3_&nmend.***
SYMBOLGEN:  Macro variable NMEND resolves to elig
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted for printing.
***tbl1_3_elig***
MLOGIC(TABLE1_3):  %PUT ***tbl1_3_&nmend.***
SYMBOLGEN:  Macro variable NMEND resolves to elig
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted for printing.
***tbl1_3_elig***
SYMBOLGEN:  Macro variable NMEND resolves to elig
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted for printing.
MPRINT(TABLE1_3):   data tbl1_3_elig;
MPRINT(TABLE1_3):   name='Dave';
MPRINT(TABLE1_3):   age=22;
SYMBOLGEN:  Macro variable NMEND resolves to elig
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted for printing.
MPRINT(TABLE1_3):   put "elig";
SYMBOLGEN:  Macro variable NMEND resolves to elig
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted for printing.
MPRINT(TABLE1_3):   put "tbl1_3_elig";
SYMBOLGEN:  Macro variable NMEND resolves to elig
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted for printing.
MPRINT(TABLE1_3):   put "tbl1_3_elig";
MPRINT(TABLE1_3):   output;
MPRINT(TABLE1_3):   run;

elig
tbl1_3_elig
tbl1_3_elig
NOTE: The data set WORK.TBL1_3_ has 1 observations and 2 variables.
NOTE: The data set WORK.ELIG has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.04 seconds
      cpu time            0.03 seconds


MLOGIC(TABLE1_3):  Ending execution.

It appears the dataset has one name, tbl1_3_elig, but I end up with 2 datasets, tbl1_3_ and elig! I have done things like this zillions of times in the past and never encountered this. Any suggestions as to what's going on? Did I inadvertently set some kind of environmental variable that's causing this?


Accepted Solutions
Solution
‎03-22-2017 09:42 AM
Valued Guide
Posts: 505

Re: Problem with macro variable resolution in dataset name

 

SAS cannot unquote two levels of quotes

data %str(cla%str(ss));
  set sashelp.class;
run;quit;

%unquote(tbl1_3_&nmend.);

I don't know what you are trying to do. :)
But DOSUBL really heps with meta data.

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.CLA has 19 observations and 5 variables.
NOTE: The data set WORK.SS has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds

This works. I unquoted

%symdel nmend
    elig
    tbl1_3_elig;

%macro datatype(type);
  %global nmend;
  %if &type=enrolled %then %do;
    %let nmend=%str(enrl);
  %end;
  %else %do;
    %let nmend=%str(elig);
  %end;
%mend datatype;

%macro table1_3;
%put ***&nmend.***;
%put ***tbl1_3_&nmend.***;
%put ***%str(tbl1_3_&nmend.)***;
  data %unquote(tbl1_3_&nmend.);
    name='Dave';
    age=22;
put "&nmend.";
put "tbl1_3_&nmend.";
put "%str(tbl1_3_&nmend.)";
    output;
  run;
%mend table1_3;



 

 

 

 

View solution in original post


All Replies
Super User
Posts: 17,849

Re: Problem with macro variable resolution in dataset name

I removed all the %STR() and didn't have any issues, with %STR() I had the same issues you did.

 

 

Super User
Posts: 10,514

Re: Problem with macro variable resolution in dataset name

What happens when you change

%let nmend=%str(elig);

 

to

%let nmend=elig;

 

and similar for ENRL?

 

What makes you think that you need %str(elig)? There are no special characters involved that I can see that would require macro quoting (the %str function).

 

Valued Guide
Posts: 505

Re: Problem with macro variable resolution in dataset name

SAS cannot unquote two levels of quotes

data %str(cla%str(ss));
  set sashelp.class;
run;quit;

%unquote(tbl1_3_&nmend.);

I don't know what you are trying to do. :)
But DOSUBL really heps with meta data.

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.CLA has 19 observations and 5 variables.
NOTE: The data set WORK.SS has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds

This works. I unquoted

%symdel nmend
    elig
    tbl1_3_elig;

%macro datatype(type);
  %global nmend;
  %if &type=enrolled %then %do;
    %let nmend=%str(enrl);
  %end;
  %else %do;
    %let nmend=%str(elig);
  %end;
%mend datatype;

%macro table1_3;
%put ***&nmend.***;
%put ***tbl1_3_&nmend.***;
%put ***%str(tbl1_3_&nmend.)***;
  data %unquote(tbl1_3_&nmend.);
    name='Dave';
    age=22;
put "&nmend.";
put "tbl1_3_&nmend.";
put "%str(tbl1_3_&nmend.)";
    output;
  run;
%mend table1_3;



 

 

Solution
‎03-22-2017 09:42 AM
Valued Guide
Posts: 505

Re: Problem with macro variable resolution in dataset name

 

SAS cannot unquote two levels of quotes

data %str(cla%str(ss));
  set sashelp.class;
run;quit;

%unquote(tbl1_3_&nmend.);

I don't know what you are trying to do. :)
But DOSUBL really heps with meta data.

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.CLA has 19 observations and 5 variables.
NOTE: The data set WORK.SS has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds

This works. I unquoted

%symdel nmend
    elig
    tbl1_3_elig;

%macro datatype(type);
  %global nmend;
  %if &type=enrolled %then %do;
    %let nmend=%str(enrl);
  %end;
  %else %do;
    %let nmend=%str(elig);
  %end;
%mend datatype;

%macro table1_3;
%put ***&nmend.***;
%put ***tbl1_3_&nmend.***;
%put ***%str(tbl1_3_&nmend.)***;
  data %unquote(tbl1_3_&nmend.);
    name='Dave';
    age=22;
put "&nmend.";
put "tbl1_3_&nmend.";
put "%str(tbl1_3_&nmend.)";
    output;
  run;
%mend table1_3;



 

 

 

 

New Contributor
Posts: 2

Re: Problem with macro variable resolution in dataset name

To all,

 

Thank you so much. I don't recall exactly what I did to arrive at this situation, but I tried many things (not, however, unquoting. That's a good rule of thumb to know) to get rid of the apparent space that was being inserted in the dataset name. My paranoia drove me to put %str()s around everything.

 

As a test, I tried the following: %let nmend = %str(EQ)elig; Then data tbl1_3&nmend. gave me 2 datasets (tbl1_3 and EQelig), whereas data %unquote(tbl1_3&nmend) gave me one dataset (tbl1_3EQelig).

PROC Star
Posts: 1,233

Re: Problem with macro variable resolution in dataset name

Agree, in the code you have shown, quoting is not needed.  With the code shown, if you %unquote() it is works, i.e.:

 

data %unquote(tbl1_3_&nmend);

 

I think this shouldn't be necessary (because it should be done automatically), but it fits with the general rule of "if you're using the macro language, and the generated SAS code looks correct, but it's not working correctly, try %unquote()". 

 

Note there are other situations where %unquote() can be used to "glue" together a token, see e.g. http://www.sascommunity.org/wiki/Resolving_a_macro_variable_within_single_quotes

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 158 views
  • 0 likes
  • 5 in conversation