BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

Hello,

Let's say that the following situation :

User define 2 macro variables : business  and private  (using %let statement ).

I want to create conditions as following:

If macro variable business>=1 then need to do some actions (see the code below).....

If macro variable private>=1 then need to do some actions (see the code below).....

What is the way to do it please?

Is there is another way or just the way I wrote?

%let business=2;
%let private=0;

%macro RRR;
%IF &business.>=1 %then %do;
proc sql;
create table tbl1 as
select make,
       sum(invoice) as  invoice
from  sashelp.cars
where type='Sports'
group by make
;
quit;

proc sql;
create table class_M as
select *
from sashelp.class
where sex='M'
;
quit;
%End;


%IF &private.>=1 %then %do;
proc sql;
create table class_F as
select *
from sashelp.class
where sex='F' and weight>=80
;
quit;

proc sql;
create table class_age_le12 as
select *
from sashelp.class
where age<=12
;
quit;
%End;
%mend RRR;
%RRR;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you are running a current version of SAS you can have the simple %IF/%THEN/%DO/%END blocks you have in open code without the need to define and call a macro.

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

The first line under %MACRO RRR;

 

IF &business>=1 then do;

IF statements can only be in a DATA step. This is not in a DATA step and won't work.

 

However, this does not have to be in a DATA step

 

%if &business>=1 %then %do;

and it needs to end with %END; and not END;

 

So you need to change this throughout your code.

--
Paige Miller
Tom
Super User Tom
Super User

If you are running a current version of SAS you can have the simple %IF/%THEN/%DO/%END blocks you have in open code without the need to define and call a macro.

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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
  • 670 views
  • 2 likes
  • 3 in conversation