Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditional Processing

Similar presentations


Presentation on theme: "Conditional Processing"— Presentation transcript:

1 Conditional Processing
4 Conditional Processing

2 Macros – So Far %macro allmns(dat);
/*get basic data on numeric variables*/ title "Numeric Variables, dataset: &dat"; proc means data=&dat; run; title; %mend;

3 Conditionally create SAS code within a macro program
Conditionally create SAS code within a macro program. Insert entire steps, entire statements, and partial statements into a SAS program.

4 Macro-Level Programming
Macro-level programming can generate code conditionally, based on: system values parameter values data values

5 Example: Orion Star submits a program every night to report daily sales. Every Friday, a second program is submitted to summarize weekly sales. Automate the application so that only one program is required.

6 The original data proc sql; title "Order dates on orion.order_fact";
select min(order_date) format= mmddyy8., max(order_date) format= mmddyy8. from orion.order_fact; select today()-max(order_date) into :diff quit; title; %put No of days between yesterday and max order date: &diff;

7 Update the data data recent_orders; set orion.order_fact;
order_date=order_date+&diff;/*make last order today*/; run; proc sql; select min(order_date) as first_date format=mmddyy10., max(order_date) as last_date format=mmddyy10. from recent_orders; ; quit;

8 Macro-Level Programming
Always print the daily report proc print data=recent_orders; where order_date="&sysdate9"d; var product_id total_retail_price; title "Daily sales: &sysdate9"; run; title; proc means data=recent_orders n sum mean; where order_date between "&sysdate9"d - 6 and "&sysdate9"d; var quantity total_retail_price; title "Weekly sales: &sysdate9"; run; title; Is it Friday? Yes

9 Conditional Processing %IF-%THEN and %ELSE statements.
Conditional processing is performed with General form of %IF-%THEN and %ELSE statements: expression can be any valid macro expression. The %ELSE statement is optional. These macro language statements can be used only inside a macro definition. %IF expression %THEN action; %ELSE action;

10 Macro Expressions Similarities to SAS expressions: arithmetic operators logical operators (Do not precede AND or OR with %.) comparison operators (symbols and mnemonics) case sensitivity special WHERE operators not valid Differences compared to SAS expressions: Character operands are not quoted. Ranges such as 1 <= &x <= 10 behave differently. The IN operator does not require parentheses.

11 Conditional Processing
Actions that can follow the keywords %THEN and %ELSE: a macro language statement a macro variable reference a macro call any text

12 Conditional Processing
The MLOGIC system option displays macro execution messages in the SAS log, including messages about the following: macro initialization parameter values results of arithmetic and logical operations macro termination General form of the MLOGIC|NOMLOGIC option: The default setting is NOMLOGIC. OPTIONS MLOGIC; OPTIONS NOMLOGIC;

13 Macro-Level Programming
Always print the daily report proc print data=recent_orders; where order_date="&sysdate9"d; var product_id total_retail_price; title "Daily sales: &sysdate9"; run; proc means data=recent_orders n sum mean; where order_date between "&sysdate9"d - 6 and "&sysdate9"d; var quantity total_retail_price; title "Weekly sales: &sysdate9"; run; Is it Friday? Yes

14 Processing Complete Steps
Method 1: Create separate macros %macro daily; proc print data=recent_orders; where order_date="&sysdate9"d; var product_id total_retail_price; title "Daily sales: &sysdate9"; run; title; %mend daily; %daily These two macros contain only sascode (text); not much different than in chapter 3. continued...

15 %macro weekly; proc means data=recent_orders n sum mean; where order_date between "&sysdate9"d - 6 and "&sysdate9"d; var quantity total_retail_price; title "Weekly sales: &sysdate9"; run; title; %mend weekly; %weekly

16 Processing Complete Steps
Method 1: Write a third macro that always calls the DAILY macro and conditionally calls the WEEKLY macro. %macro reports; %daily %if &sysday=Friday %then %weekly; %mend reports; %reports This is a true macro program, with a macro call and a macro language statement. This macro contains no sascode (text). This is a "system driven" macro insofar as it is driven by or makes a decision according to system information, such as the day of the week. Note: Character constants are not quoted Character constants are case sensitive.

17 The mlogic option %macro reports; %daily
%if &sysday=Friday %then %weekly; %mend reports; options mlogic; %reports options nomlogic;

18 The MPRINT option (review)
%macro reports; %daily %if &sysday=Friday %then %weekly; %mend reports; options mprint; %reports options nomprint;

19 %macro reports; %daily %if &sysday=Friday %then %weekly; %mend reports; options mlogic mprint; %reports options nomlogic nomprint;

20 A commonly made error. %macro reports; %daily
%if &sysday=Friday then %weekly; %mend reports; Type answer here

21 Processing Complete Steps
Method 1: Write a third macro that always calls the DAILY macro and conditionally calls the WEEKLY macro. %macro reports; %daily %if &sysday=Friday %then %weekly; %mend reports; %reports This is a true macro program, with a macro call and a macro language statement. This macro contains no sascode (text). This is a "system driven" macro insofar as it is driven by or makes a decision according to system information, such as the day of the week. Note: Character constants are not quoted Character constants are case sensitive.

22 Conditional Processing %IF-%THEN and %ELSE statements.
Conditional processing is performed with General form of %IF-%THEN and %ELSE statements: expression can be any valid macro expression. The %ELSE statement is optional. These macro language statements can be used only inside a macro definition. %IF expression %THEN action; %ELSE action;

23 Conditional Processing
Use %DO and %END statements following %THEN or %ELSE to generate text that contains semicolons. %IF expression %THEN %DO; statement; statement;... %END; %ELSE %DO;

24 Processing Complete Steps
Method 2: Use a single macro to generate the daily report unconditionally and the weekly report on Friday. %macro reports; proc print data=recent_orders; where order_date="&sysdate9"d; var product_id total_retail_price; title "Daily sales: &sysdate9"; run; title; %if &sysday=Friday %then %do; proc means data=recent_orders n sum mean; where order_date between "&sysdate9"d - 6 and "&sysdate9"d; var quantity total_retail_price; title "Weekly sales: &sysdate9"; %end; %mend reports; %reports Here is a second approach to the same application ("Method 2"). The advantage is that this is a single macro, not three macros as in "Method 1". This is a common technique.

25 The %INCLUDE Statement
The %INCLUDE statement retrieves SAS source code from an external file and places it on the input stack. %INCLUDE file-specification < / SOURCE2 >; file-specification is the physical name or fileref of the file to be retrieved and placed on the input stack. SOURCE2 requests inserted SAS statements to appear in the SAS log.

26 Processing Complete Statements
Method 3: Store the production SAS programs in external files. Copy those files to the input stack with %INCLUDE statements. %let path=c:\users\dlm1\dropbox\tmp; options macrogen; %macro reports; %include "&path\daily.sas"; %if &sysday=Friday %then %do; %include "&path\weekly.sas"; %end; %mend reports; %reports options nomacrogen; This is a modular approach. Warning: %INCLUDE may LOOK like a macro language statement, but it is NOT. It is a global SAS statement. Offer your sincere apologies to anyone who is confused.

27 The %INCLUDE Statement
The %INCLUDE statement retrieves SAS source code from an external file and places it on the input stack. %INCLUDE file-specification < / SOURCE2 >; file-specification is the physical name or fileref of the file to be retrieved and placed on the input stack. SOURCE2 requests inserted SAS statements to appear in the SAS log.

28 %let path=c:\users\dlm1\dropbox\tmp;
%macro reports; %include "&path\daily.sas" /source2; %if &sysday=Friday %then %do; %include "&path\weekly.sas" /source2; %end; %mend reports; %reports

29 The %INCLUDE Statement
The %INCLUDE statement Copies SAS statements from an external file to the input stack Is a global SAS statement Is not a macro language statement

30 Processing Complete Statements

31 A useful feature multiple where statements
data tmp; set fram.frex4; where age >50; where male; run; proc freq data=tmp; tables male; proc means data=tmp; var age;

32 The same keyword data tmp; set fram.frex4; where age >50;
where same and male; run; proc freq data=tmp; tables male; proc means data=tmp; var age;

33 Processing Complete Statements
%macro count(type=,start=01jan2014,stop=31dec2014); proc freq data=recent_orders; where order_date between "&start"d and "&stop"d; table quantity; title1 "Orders from &start to &stop"; %if &type= %then %do; title2 "For All Order Types"; %end; %else %do; title2 "For Order Type &type Only"; where same and order_type=&type; run; title; %mend count; Note the SAME AND argument in the highlighted WHERE statement. The log will contain a note that the WHERE clause has been augmented. The affect is like "ANDing" the WHERE clauses together. This is a "parameter driven" macro insofar as it is driven by or makes a decision according to a parameter value.

34 options mprint mlogic;
%count() %count(type=3) options nomprint nomlogic;

35 Processing Complete Statements
%macro cust(place); %let place=%upcase(&place); data customers; set orion.customer; %if &place=US %then %do; where country='US'; keep customer_name customer_address country; %end; %else %do; where country ne 'US'; keep customer_name customer_address country location; length location $ 12; if country="AU" then location='Australia'; else if country="CA" then location='Canada'; else if country="DE" then location='Germany'; else if country="IL" then location='Israel'; else if country="TR" then location='Turkey'; else if country="ZA" then location='South Africa'; run; %mend cust; Processing Complete Statements This example is conceptually no different than the previous example. The previous example featured a proc step and this example features a data step. Otherwise, same concept. One purpose here is to present both %if-%then and if-then in the same program. Students should understand the difference. An upcoming quiz addresses this. Also note the parameter values on the macro call are typed in lower case. Would it make a difference if they were typed in upper case or mixed case? No. Note the %upcase function at the top of the macro. One way to look at this application, as well as the previous applications in this chapter, is that this program is really two programs in one. Depending on the parameter value, one DATA step is generated, or an entirely different DATA step is generated. This is clearly illustrated on the next two slides. The macro language makes this benefit possible.

36 options mprint; %cust(us) proc sql; select distinct country from customers;quit; %cust(international) from customers; quit; options nomprint;

37 Processing Partial Statements

38 Processing Partial Statements
Conditionally insert text into the middle of a statement. Generate either a one-way or two-way frequency table, depending on parameter values. options mprint; %macro counts(rows); title 'Customer Counts by Gender'; proc freq data=orion.customer_dim; tables %if &rows ne %then &rows *; customer_gender; run; %mend counts; %counts() %counts(customer_age_group) options nomprint; Here again, a single program is really two programs in one. Made possible by the macro facility.


Download ppt "Conditional Processing"

Similar presentations


Ads by Google