Data-Driven Macro Calls

You can create data-driven macro calls using the DOSUBL DATA step function.

A Brief Overview

The DOSUBL function enables the immediate execution of SAS code after a text string is passed. Macro variables that are created or updated during the execution of the submitted code are exported back to the calling environment.
DOSUBL returns a value of 0 if SAS code was able to execute, and returns a nonzero value if SAS code was not able to execute.
DOSUBL should be used in a DATA step. It can also be used with %SYSFUNC outside a step boundary.

The DOSUBL Function

The DOSUBL function imports macro variables from the calling environment, and exports macro variables back to the calling environment.
Syntax, DOSUBL function:
DOSUBL(text-string)
text string
specifies the SAS code to run within the DOSUBL function.
Note: In older macro programs, you might see CALL EXECUTE used in a similar manner to generate macro calls. CALL EXECUTE can encounter timing errors if the DATA step using CALL EXECUTE was itself generated by a macro program. DOSUBL avoids this issue.

Example: Using the DOSUBL Function

Suppose you create a macro called DelayReport that executes a PROC SQL query that joins two tables together.
%macro DelayReport(empid);
title "Flight Delays for Employee &Empid";
proc sql;
   select DelayCategory, Count(*) as Count
      from 
         certadv.flightdelays d
         inner join 
         certadv.flightschedule s
      on s.date=d.date and s.flightnumber=d.flightnumber
      where empid="&Empid"
      group by DelayCategory
;
quit;
title;
%mend;
Suppose you want to pull the report for each EmpId in Certadv.FlightCrewNew. Without a data-driven program you would manually call the macro for each EmpId.
%Delayreport(1928)
%Delayreport(1407)
%Delayreport(1574)
%Delayreport(1777)
Certadv.FlightCrewNew has only four observations, so manually calling the macro is not too tedious. However, if your data set had hundreds of observations, manually calling the macro for each EmpId would be time consuming. Instead, you can create a data-driven program that uses the DOSUBL function to call the DelayReport macro and generate results for each EmpId.
The DOSUBL function uses the value found in EmpID concatenated between '%DelayReport (' and ')' to generate a valid macro call.
data _null_;
   set certadv.FlightCrewNew;
   rc=dosubl(cats('%DelayReport(',empid,')'));
run;
Output 10.2 Data-Driven Macro Call Result
Data-Driven Macro Call Result
Last updated: October 16, 2019
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.40.171