Creating Multiple Macro Variables during DATA Step Execution

Creating Multiple Macro Variables with CALL SYMPUT

Sometimes you might want to create multiple macro variables within one DATA step. For example, suppose you want to write a program that lists all of the scheduled dates for a particular course, using a macro variable to record the title of the course.
%let crsid=C005;
data _null_;
   set sasuser.courses;
   where course_code="&crsid";
   call symput('title',trim(course_title));
run;
    
proc print data=sasuser.schedule noobs label;
   where course_code="&crsid";
   var location begin_date teacher;
   title1 "Schedule for &title";
   options nodate nonumber;
run;
In this example, the value of the data set variable Course_title for the course whose Course_code is C005 is assigned as a value for the macro variable title. The value _null_ on the data statement is used because we do not need a data set to be created in this example.
In order to create a listing for a different course, you would need to change the %LET statement and resubmit the DATA step to assign a new value to title. Then you would need to resubmit the PROC PRINT step. Although you would need to resubmit both the DATA step and the PROC PRINT step, these two steps would be identical to the steps that you submitted for the first report. This is an extremely inefficient program.
%let crsid=C004;
data _null_;
   set sasuser.courses;
   where course_code="&crsid";
   call symput('title',trim(course_title));
run;

proc print data=sasuser.schedule noobs label;
   where course_code="&crsid";
   var location begin_date teacher;
   title1 "Schedule for &title";
   options nodate nonumber;
run;
Instead of executing separate DATA steps to update the same macro variable, you can create related macro variables in one DATA step. To create multiple macro variables, you use the SYMPUT routine with DATA step expressions for both arguments.
General form, SYMPUT routine with DATA step expressions:
CALL SYMPUT(expression1,expression2);
Here is an explanation of the syntax:
expression1
evaluates to a character value that is a valid macro variable name. This value should change each time you want to create another macro variable.
expression2
is the value that you want to assign to a specific macro variable.

Example

In this example, you use one call to the SYMPUT routine in order to create one macro variable for each value of the DATA step variable Course_code and to assign the corresponding value of Course_title to each macro variable. That is, for each observation in Sasuser.Courses, the macro processor creates a new macro variable. The new macro variable has the same name as the value of the data set variable Course_code for that observation. The value of the new macro variable is the value of the data set variable Course_title for that observation.
data _null_;
   set sasuser.courses;
   call symput(course_code, trim(course_title));
run;
%put _user_;
The SAS log shows that six observations were read from the data set Sasuser.Courses and that six global macro variables were created and were assigned values.
Table 10.4 SAS Log
2   data _null_;
3      set sasuser.courses;
4      call symput(course_code, trim(course_title));
5   run;

NOTE: There were 6 observations read from the dataset 
      SASUSER.COURSES.
NOTE: DATA statement used:
      real time           0.52 seconds
      cpu time            0.13 seconds

7   %put _user_;
GLOBAL C006 Computer Aided Design
GLOBAL C001 Basic Telecommunications
GLOBAL C002 Structured Query Language
GLOBAL C003 Local Area Networks
GLOBAL C004 Database Design
GLOBAL C005 Artificial Intelligence
You can then use these new macro variables to print listings of information for various courses, using only one DATA step, as follows:
data _null_;
   set sasuser.courses;
   call symput(course_code,trim(course_title));
run; 

%let crsid=C005;
proc print data=sasuser.schedule noobs label;
   where course_code="&crsid";
   var location begin_date teacher;
   title1 "Schedule for &c005";
run;

%let crsid=C002;
proc print data=sasuser.schedule noobs label;
   where course_code="&crsid";
   var location begin_date teacher;
   title1 "Schedule for &c002";
run;
This is the output from the first PROC PRINT step.
Schedule for Artificial Intelligence
This is the output from the second PROC PRINT step.
Schedule for Structured query language
The program in this section is more efficient than the program shown in the previous section since the Sasuser.Courses data set is read only once in the latest example. However, there is still room for improvement.
..................Content has been hidden....................

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