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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
rogerjdeangelis
Barite | Level 11

 

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

6 REPLIES 6
Reeza
Super User

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

 

 

ballardw
Super User

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).

 

rogerjdeangelis
Barite | Level 11
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;



 

 

rogerjdeangelis
Barite | Level 11

 

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;



 

 

 

 

jman
Calcite | Level 5

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).

Quentin
Super User

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

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 3756 views
  • 0 likes
  • 5 in conversation