BookmarkSubscribeRSS Feed
suresh123
Calcite | Level 5

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
20 REPLIES 20
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

suresh123
Calcite | Level 5
I trying to include these two macros in the codes without disturbing it much to get the required output
Astounding
PROC Star

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?

Tom
Super User Tom
Super User

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?

suresh123
Calcite | Level 5
actually it’s small bit of code. I didn’t include every variables and code. If it is linear it gives different report and for non linear different report but both looks almost same. Iam just trying include linear with non segment and non linear with non segment in the code using macro definition( will change macro definition based on non segment or segment before running the code)
suresh123
Calcite | Level 5
Non segment is macro variable which works only when I define it as Y
Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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;
suresh123
Calcite | Level 5
Yes if it is Y then output only first observation
Astounding
PROC Star

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?

 

 

suresh123
Calcite | Level 5

I am getting below error.

 

proc print data=new;
89 %if &Non_segment = Y %then (obs=1);
ERROR: Expected %DO not found.
ERROR: Skipping to next %END statement.
90 ;
91 id direction segment;
92 var accts;
93 run;
suresh123
Calcite | Level 5
So I want include non segment and direction in both linear and non linear
suresh123
Calcite | Level 5
From above I can understand that I can use if condition in proc print
Astounding
PROC Star

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 20 replies
  • 2099 views
  • 0 likes
  • 4 in conversation