BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want to ask a general question.

Many times I am asked to create a summary report for different populations.

In order to do it efficiency I am using macro.

In the Macro I define a parameter that define the population.

The problem is that sometimes I need that there will not be any rows screen.

The code here just show you what i want but the way it is written is wrong

 

for example:

Data tbl;
input ID X $ Z;
cards;
1  a 2 
2  a 2
3  a 1
4  b 1
5  b 2
6  c 2
7  c 1
8  c 1
9  c 2
10 c 2
;
run;

%macro mmacro(condition);
PROC SQL;
	create table xxx as
	select 	   
	from tbl
	where &condition.
;
QUIT;
%mend;

/*Run1*/
%mmacro(condition=(x='a' and z='1');
/*Run2*/
%mmacro(condition=(x='a' and z='2');
/*Run3*/
%mmacro(condition=((x='b' OR x='c') and z='2');
/*Run4*/
%mmacro(condition=((x='b' OR x='c'));
/*Run5*/
%mmacro(condition=((x='b' OR x='c'));
/*Run6*/
%mmacro(condition=((no condition));

 

2 REPLIES 2
Astounding
PROC Star

Since you are already working within a macro, the change should be straightforward.  You already have this:

 

where condition=&condition.

 

Change it to:

 

%if %length(&condition) %then where condition=&condition.;

 

You still need the semicolon as the next line to end the SELECT statement.  On this replacement line, the semicolon ends the %IF %THEN statement.

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

I would suggest your thinking about it in "not the SAS way".  Step 1 assign groups, Step 2 freq or means or whichever, by groups.  E.g.

Data tbl;  input ID X $ Z;
if x="a" and z=1 then group=1;
if...; cards; 1 a 2 2 a 2 3 a 1 4 b 1 5 b 2 6 c 2 7 c 1 8 c 1 9 c 2 10 c 2 ; run;

For example, a simple illustration based on inbuilt table:

data class;
  set sashelp.class;
  if age <12 then do;
    agegrp="Up to 12";
    output;
  end;
  else if 12 <= age < 14 then do;
    agegrp="Up to 14";
    output;
  end;
  else do;
    agegrp="All";
    output;
  end;
run;
proc freq data=class noprint;
  tables agegrp*sex / out=want;
run;

This is far more resource efficient, easier to code, and more robust.

 

Do note there are a fair few logical errors in your code also.  The create table will overwrite each time.  Variable Z is a numeric variable, you are logically trying to match to a text string with:

and z='1');

Not a good idea.

 

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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