Below is the small piece of code to get reports in excel.
%if &linear %then %do;
ods excel options(sheet_name="vol");
proc print data=perf;
id direction segment;
var accts;
run;
%end;
%else %do;
ods excel options(sheet_name="vol");
proc print data=perf;
id direction segment;
var accts;
run;
%end;
Direction segment accts
A model 17177
A booked 567
A unbooked 5676
B model 17177
B booked 567
B unbooked 5676
If segments are not available i will get report as below
Direction segment accts
A model 17177
A 1 17177
B model 17177
B 1 17177
Iam planing to introduce two macro variables
%let dir =A;
%let Non_segment=y;
Based on value for direction it should give only those direction and if there no segment(Non_segment=y;), it should have only first observation. So the output will looks like below for Non_segment=y
Direction segment accts
A model 17177
And the question is?
Remembering that macro is nothing more than a find/replace system, what would the code look like without all the macro? Once that is running then macrotise it.
Let's back up for a moment.
You have two sections of code, one when &LINEAR is true and one when it is false. Yet the code for both of them is identical. Why are there two sections of code instead of one?
Is NON_SEGMENT a variable within PERF, or is it a macro variable?
I don't understand what you are asking.
The first part makes no sense because the macro code is not doing anything. It is generating the exact same code in the THEN and ELSE branches.
The second part also doesn't make a lot of sense. Perhaps you have left out a step?
Do you just want to exclude the rows where SEGMENT='1'?
Do you want to do that conditionally? Or all of the time?
Using macro language, this sort of thing is not difficult once you have seen it. For example, this code could be inserted within PROC PRINT:
%if %length(&dir) %then %do;
where direction = "&dir";
%end;
Depending on whether &DIR has a value or not, macro language is able to insert a WHERE statement within PROC PRINT.
The basic idea is that you have to know what the final SAS code should look like, and then get macro language to generate the appropriate SAS code.
What are you trying to use the macro variable NON_SEGMENT to do? How you use it determines how you should set its value. (or if instead you know what values it will have that will determine how you use it).
For example if you wanted to allow both Y and y as the value of NON_SEGMENT then your macro logic might look like this:
%if %upcase(&non_segment)=Y %then %do;
....
%end;
In every case, you need to know what the SAS code would look like (without any macro language involved). The role of macro language will be to generate the proper SAS code. For example, you might decide that this would be the way to print the first observation only:
proc print data=perf (obs=1);
id direction segment;
var accts;
run;
In that case, you could modify your code using macro language to look like this:
proc print data=perf
%if &Non_segment = Y %then (obs=1);
;
id direction segment;
var accts;
run;
So if you want to include nonsegment and direction, what would the SAS code look like without macro language?
I am getting below error.
You can never use an if condition in PROC PRINT.
You can use macro language (including %if %then) to construct the statements that become part of your program.
Macro language is a tool unto itself. It's not something you can learn by asking a few questions here and there. It typically takes two full days in a classroom to cover an introduction to macro language. There is good documentation available, if it is something you want to learn on your own. It is advisable that you have a good grasp of SAS language first, because the whole purpose of macro language is to build a SAS program. You have to be able to envision what the SAS program should look like, for macro language to be useful.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.