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

Dear SAS experts

 

I am relatively new to SAS. I am trying to build a macro in which I generate an Excel table using ods and proc tabulate. The code written in a general way and using a sashelp dataset is as follows;

 

%macro table(specify=);

 

ods excel close;
ods excel file='pathspecification/sometext_&specify.xlsx';

 

proc tabulate data=sashelp.baseball missing;
class Position League;
table (all='All persons in the &specify division')
position,
League ALL/misstext='0';
where Division=&specify;
run;

 

ods excel close;

 

%mend;

 

%table(specify='EAST');

 

As shown I would like to insert 'EAST' in the title of the new excel file, in some text to the left in the table and I would also like to use it to specify a subset of the dataset for the table. It appears that I can only get the last of these three to work. My issue is probably related to the fact that it is necessary to write the macro code in a certain way within fields including ' '. I have tried a few things but I have not yet had real success with getting the code to work. 

 

Can anyone help?

 

Note that the "code itself" works when not specied within a macro.

 

Best regards

 

Martin

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
  • Macro variables only resolve in double quotes not single quotes so change your code to use double quotes.
  • Note that it will also resolve with the quotation marks and you likely don't want that so I'd remove the quotation marks from your macro variable.
  • The WHERE clause needs to include the double quotes.
  • Periods indicate the end of a macro variable so you also need to add two periods, one for the extension and one for the end of the variable.

 

%macro table(specify=);

ods excel file="pathspecification/sometext_&specify..xlsx";

 

proc tabulate data=sashelp.baseball missing;
class Position League;
table (all='All persons in the &specify division')
position,
League ALL/misstext='0';
where Division="&specify";
run;

 

ods excel close;

 

%mend;

 

%table(specify=EAST);

@mgrasmussen wrote:

Dear SAS experts

 

I am relatively new to SAS. I am trying to build a macro in which I generate an Excel table using ods and proc tabulate. The code written in a general way and using a sashelp dataset is as follows;

 

%macro table(specify=);

 

ods excel close;
ods excel file='pathspecification/sometext_&specify.xlsx';

 

proc tabulate data=sashelp.baseball missing;
class Position League;
table (all='All persons in the &specify division')
position,
League ALL/misstext='0';
where Division=&specify;
run;

 

ods excel close;

 

%mend;

 

%table(specify='EAST');

 

As shown I would like to insert 'EAST' in the title of the new excel file, in some text to the left in the table and I would also like to use it to specify a subset of the dataset for the table. It appears that I can only get the last of these three to work. My issue is probably related to the fact that it is necessary to write the macro code in a certain way within fields including ' '. I have tried a few things but I have not yet had real success with getting the code to work. 

 

Can anyone help?

 

Note that the "code itself" works when not specied within a macro.

 

Best regards

 

Martin


 

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

The values of a macro variable should not be in quotes. There is almost never a reason for the value of a macro variable to begin with a quote and end with a quote.

 

Aside from the quotes (as mentioned above) SPECIFY='EAST' is always wrong for this data set. There is no observation in SASHELP.BASEBALL where DIVISION='EAST', please look carefully.

 

To resolve macro variables in your SAS code, you need to include the macro variable name in double-quotes.

 

 

%macro table(specify=);

ods excel file="pathspecification/sometext_&specify..xlsx";
proc tabulate data=sashelp.baseball missing;
class Position League;
table (all="All persons in the &specify division")
position,
League ALL/misstext='0';
where Division="&specify";
run;
ods excel close;
%mend;

%table(specify=East)

 

 

Please also understand that macro variables only perform text substitution. The macro variable is replaced by its value when the code is run, and this must result in legal, working, valid SAS code, and you have not written a macro that produces legal valid working SAS code when the macro variables are replaced by their value.

 

--
Paige Miller
Reeza
Super User
  • Macro variables only resolve in double quotes not single quotes so change your code to use double quotes.
  • Note that it will also resolve with the quotation marks and you likely don't want that so I'd remove the quotation marks from your macro variable.
  • The WHERE clause needs to include the double quotes.
  • Periods indicate the end of a macro variable so you also need to add two periods, one for the extension and one for the end of the variable.

 

%macro table(specify=);

ods excel file="pathspecification/sometext_&specify..xlsx";

 

proc tabulate data=sashelp.baseball missing;
class Position League;
table (all='All persons in the &specify division')
position,
League ALL/misstext='0';
where Division="&specify";
run;

 

ods excel close;

 

%mend;

 

%table(specify=EAST);

@mgrasmussen wrote:

Dear SAS experts

 

I am relatively new to SAS. I am trying to build a macro in which I generate an Excel table using ods and proc tabulate. The code written in a general way and using a sashelp dataset is as follows;

 

%macro table(specify=);

 

ods excel close;
ods excel file='pathspecification/sometext_&specify.xlsx';

 

proc tabulate data=sashelp.baseball missing;
class Position League;
table (all='All persons in the &specify division')
position,
League ALL/misstext='0';
where Division=&specify;
run;

 

ods excel close;

 

%mend;

 

%table(specify='EAST');

 

As shown I would like to insert 'EAST' in the title of the new excel file, in some text to the left in the table and I would also like to use it to specify a subset of the dataset for the table. It appears that I can only get the last of these three to work. My issue is probably related to the fact that it is necessary to write the macro code in a certain way within fields including ' '. I have tried a few things but I have not yet had real success with getting the code to work. 

 

Can anyone help?

 

Note that the "code itself" works when not specied within a macro.

 

Best regards

 

Martin


 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 921 views
  • 0 likes
  • 3 in conversation