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 would like to create a table where the title is defined depending on the macro parameter specified.
Below is a simple example of scenario. If "M" is specified I would like a title specific for "M",
and the same for "F". I cannot get it to work, but I suspect I need to somehow use %if and %else in the macro.
Can someone help with the code? Thanks.

 

data mydata;
set sashelp.class;
run;

 

*Creating tables;
%macro table(Sex=);

proc means data=mydata;
var Age;
where Sex="&Sex.";
%if &Sex.="M" %then %do;
Title "Table of: &Sex. (M text)";
%else %do;
Title "Table of: &Sex. (F text)";
%end;
run;

 

%mend;

 

%table(Sex=M);


%table(Sex=F);

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

You have left out an %end; statement.

 

%macro table(Sex=);
    proc means data=mydata;
        var Age;
        where Sex="&Sex.";
        %if &Sex.="M" %then %do;
            Title "Table of: &Sex. (M text)";
        %end; /* This was left out and causes the problems */
        %else %do;
            Title "Table of: &Sex. (F text)";
        %end;
    run;
%mend;
%table(Sex=M)
%table(Sex=F)

If you indent your code properly, as I have done, these types of errors become more obvious.

 

You don't really need a macro for this, simply a macro variable.

 

%let sex=M;
proc means data=sashelp.class;
    var Age;
    where Sex="&sex";
    Title "Table of: &sex";
run;
--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

You have left out an %end; statement.

 

%macro table(Sex=);
    proc means data=mydata;
        var Age;
        where Sex="&Sex.";
        %if &Sex.="M" %then %do;
            Title "Table of: &Sex. (M text)";
        %end; /* This was left out and causes the problems */
        %else %do;
            Title "Table of: &Sex. (F text)";
        %end;
    run;
%mend;
%table(Sex=M)
%table(Sex=F)

If you indent your code properly, as I have done, these types of errors become more obvious.

 

You don't really need a macro for this, simply a macro variable.

 

%let sex=M;
proc means data=sashelp.class;
    var Age;
    where Sex="&sex";
    Title "Table of: &sex";
run;
--
Paige Miller
mgrasmussen
Quartz | Level 8

Dear PaigeMiller 

 

Thanks.

 

I was not aware of the fact that I also needed a "%end" there. So, what was lacking was because of ignorance.

 

Regarding your second point, I want to include some additional text and not just the "&Sex." part. In the actual data what I want to specificy is that the statistics I am summarizing are based on different age group restrictions depending on the (disease) subgroup of the population. Therefore, I would like to be able to some how write this out.

PaigeMiller
Diamond | Level 26

@mgrasmussen wrote:

Regarding your second point, I want to include some additional text and not just the "&Sex." part. In the actual data what I want to specificy is that the statistics I am summarizing are based on different age group restrictions depending on the (disease) subgroup of the population. Therefore, I would like to be able to some how write this out.


Be specific. Show us an actual example (or even a made up example) of what you are talking about here. Don't start us off with a problem that isn't really what you are trying to do, because obviously solving that original problem hasn't helped you.

--
Paige Miller
PaigeMiller
Diamond | Level 26

@mgrasmussen wrote:

 

I was not aware of the fact that I also needed a "%end" there. So, what was lacking was because of ignorance.


Every %DO has to have an associated %END;

--
Paige Miller
PaigeMiller
Diamond | Level 26

@mgrasmussen 

All of which brings up the interesting, and extremely important question: why would you use a macro at all here? Why don't you just use a BY statement in PROC MEANS? This is probably the easiest way to do this. Splitting data sets up using macros or macro variables is usually a poor choice when BY processing is possible.

 

proc sort data=sashelp.class out=class;
    by sex;
run;

options nobyline;
ods noproctitle;
title "Table of #byval1";
proc means data=class;
    var age;
    by sex;
run;

 

 

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 1084 views
  • 0 likes
  • 2 in conversation