Creating Macro Variables during DATA Step Execution

The CALL SYMPUTX Routine

A Brief Overview

The CALL SYMPUTX routine does the following:
  • assigns a value produced in a DATA step to a macro variable
  • if a macro variable does not exist, the CALL SYMPUTX routine creates the variable
  • creates a macro variable assignment when the program is executed
  • removes both leading and trailing blanks from both arguments
Note: The CALL SYMPUT routine is an older version of CALL SYMPUTX. CALL SYMPUT has fewer features.

CALL SYMPUTX Routine Syntax

Syntax, CALL SYMPUTX routine:
CALL SYMPUTX(macro-variable-name, value <,symbol-table>);
macro-variable-name
is assigned the character value ofexpression, and any leading or trailing blanks are removed from both macro variable and expression.
macro-variable and expression
can each be specified as one of the following items:
  • a literal, enclosed in quotation marks
  • a DATA step variable
  • a DATA step expression
value
is the value to be assigned, which can be any of these items:
  • a string enclosed in quotation marks.
  • the name of a numeric or character variable. The current value of the variable is assigned as the value of the macro variable. If the variable is numeric, SAS performs an automatic numeric-to-character conversion and writes a message in the log.
    Note: This form is most useful when macro-variable is also the name of a SAS variable or a character expression that contains a SAS variable. A unique macro variable name and value can be created from each observation.
  • a DATA step expression. The value returned by the expression in the current observation is assigned as the value of macro-variable. If the expression is numeric, SAS performs an automatic numeric-to-character conversion and writes a message in the log.
symbol-table
specifies a character constant, variable, or expression. The value of symbol-table is not case sensitive. The first non-blank character in symbol-table specifies the symbol table in which to store the macro variable. The following values are valid as the first non-blank character in symbol-table:
G
specifies that the macro variable is stored in the global symbol table, even if the local symbol table exists.
L
specifies that the macro variable is stored in the most local symbol table that exists. That is the global symbol table, if it is used outside a macro.
F
specifies that if the macro variable exists in any symbol table, CALL SYMPUTX uses the version in the most local symbol table in which it exists. If the macro variable does not exist, CALL SYMPUTX stores the variable in the most local symbol table.

Using SYMPUTX with a Literal

In the SYMPUTX routine, you use a literal string in the following places:
  • the first argument, to specify an exact name for the name of the macro variable
  • the second argument, to specify the exact character value to assign to the macro variable
To use a literal with the SYMPUTX routine, you enclose the literal string in quotation marks.
CALL SYMPUTX('macro-variable', 'text');

Using CALL SYMPUTX Routine with a DATA Step Variable

You can assign the value of a DATA step variable as the value for a macro variable by using the DATA step variable's name as the second argument in a CALL SYMPUTX routine.
To use a DATA step variable as the value for a macro variable in a CALL SYMPUTX routine, place the name of the DATA step variable after the name of the macro variable, separated by a comma. You do not enclose the name of the DATA step variable in quotation marks.
CALL SYMPUTX('macro-variable',DATA-step-variable);
This form of the CALL SYMPUTX routine creates a macro variable named macro-variable and assigns to it the current value of DATA-step-variable.
Using a DATA step variable as the second argument has these results:
  • A maximum of 32,767 characters can be assigned to the receiving macro variable.
  • Any leading or trailing blanks that are part of the DATA step variable's value are stored in the macro variable.
  • Values of numeric variables are automatically converted to character values, using the BEST12. format.
CAUTION:
If you enclose the DATA step variable name in quotation marks, SAS interprets the name as a literal value rather than as a variable name, and the DATA step variable's value is not resolved.

Example: Using the CALL SYMPUTX Routine

Suppose you want the title to contain the course name and the course number, as well as the date on which the course was held. You also want the footnote to list the current amount of unpaid fees for the course.
This example creates three macro variables. The macro variable Csrname records the value of the DATA step variable Course_title. The macro variable date records the value of the DATA step variable Begin_date in MMDDYY10. format. Finally, the macro variable Due uses the values of the DATA step variables Paidup, Total, and Fee to record the current amount of unpaid fees in DOLLAR8. format. These macro variables are referenced later in the program in the TITLE and FOOTNOTE statements.
%let crsnum=3;
data revenue;
   set certadv.all end=final;
   where course_number=&crsnum;
   total+1;
   if paid='Y' then paidup+1;
   if final then do;
      call symputx('crsname',course_title);
      call symputx('date',put(begin_date,mmddyy10.));
      call symputx('due',put(fee*(total-paidup),dollar8.));
   end;
run;
proc print data=revenue;
   var student_name student_company paid;
   title "Fee Status for &crsname (#&crsnum) Held &date";
   footnote "Note: &due in Unpaid Fees";
run;
Output 8.5 PROC PRINT Output of Work.Revenue
PROC PRINT Result of Work.Revenue

Example: Using the CALL SYMPUTX Routine with Literal Strings

Use the CALL SYMPUTX routine with literal strings as both arguments in order to conditionally assign a value to the macro variable Foot based on values that are generated during DATA step execution.
options symbolgen pagesize=30;
%let crsnum=3;
data revenue;
   set certadv.all end=final;
   where course_number=&crsnum;
   total+1;
   if paid='Y' then paidup+1;
   if final then do;
   if paidup<total then do;
      call symputx('foot','Some Fees Are Unpaid');
   end;
   else do;
      call symputx('foot','All Students Have Paid');
   end;
end;
run;

proc print data=work.revenue;
   var student_name student_company paid;
   title "Payment Status for Course &crsnum";
   footnote "&foot";
run;
This time, the value that is assigned to Foot is either Some Fees Are Unpaid or All Students Have Paid, depending on the value of the DATA step variable Paidup, because the value is assigned during the execution of the DATA step. When you submit this code, you get the following output.
Output 8.6 PROC PRINT Output for Work.Revenue
PROC PRINT Output of Work.Revenue

Example: Using the CALL SYMPUTX Routine with a DATA Step Variable

Once again, suppose you want to create a report about students who are enrolled in a particular course. This time, suppose you want to add a title that contains the course title and the course number, and you want to include a footnote that summarizes how many students have paid their fees.
In this example, a DATA step variable named Paidup records the number of students who have paid, and a DATA step variable named Total records the total number of students who are registered for the class. Macro variables are created to record the values of Paidup, the value of Total, and the value of Course_title. These macro variables are referenced later in the program.
%let crsnum=3;
data revenue;
   set certadv.all end=final;
   where course_number=&crsnum;
   total+1;
   if paid='Y' then paidup+1;
   if final then do;
      call symputx('numpaid',paidup);
      call symputx('numstu',total);
      call symputx('crsname',course_title);
   end;
run;
proc print data=revenue noobs;
   var student_name student_company paid;
   title "Fee Status for &crsname (#&crsnum)";
   footnote "Note: &numpaid Paid out of &numstu Students";
run;
This time, the footnote shows the correct information for how many students have paid.
Output 8.7 PROC PRINT Output for Work.Revenue with Correct Footnote
PROC PRINT Output of Work.Revenue With Correct Footnote

Example: Creating Multiple Macro Variables with CALL SYMPUTX

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. You can use one call to the SYMPUTX routine to create a macro variable for each value of the DATA step variable Course_code and assign the corresponding value of Course_title to each macro variable. The macro processor creates a new macro variable for each observation. 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 certadv.courses;
   call symputx(course_code,course_title);
run;
%put _user_;
The following is written to the SAS log.
Log 8.15 SAS Log
GLOBAL A one:two-three four
GLOBAL C001 Basic Telecommunications
GLOBAL C002 Structured Query Language
GLOBAL C003 Local Area Networks
GLOBAL C004 Database Design
GLOBAL C005 Artificial Intelligence
GLOBAL C006 Computer Aided Design
GLOBAL CRSNAME Local Area Networks
GLOBAL CRSNUM 3
GLOBAL DELIM 
GLOBAL NUMPAID           14
GLOBAL NUMSTU           20
GLOBAL PATH C:Userscertadv
GLOBAL STRING william SMITH

%let crsid=C005;
proc print data=certadv.schedule noobs label;
   where course_code="&crsid";
   var location begin_date teacher;
   title1 "Schedule for &c005";
run;
Output 8.8 PROC PRINT Output for Schedule for Artificial Intelligence
PROC PRINT Output of Schedule for Artificial Intelligence
%let crsid=C002;
proc print data=certadv.schedule noobs label;
   where course_code="&crsid";
   var location begin_date teacher;
   title1 "Schedule for &c002";
run;
Output 8.9 PROC PRINT Output of Schedule for Structured Query Language
PROC PRINT Output of Schedule for Structured Query Language

The PUT Function

A Brief Overview

Messages are written to the SAS log to alert you that automatic conversion has occurred. Remember that the CALL SYMPUTX routine automatically uses the BEST12. format for the conversion.
You might want to have explicit control over the numeric-to-character conversion. The PUT function returns a character string that is formed by writing a value with a specified format.
You can use the PUT function in the following ways:
  • perform explicit numeric-to-character conversions
  • format the result of a numeric expression

PUT Function Syntax

Syntax, PUT function:
PUT(source,format.)
source
is a constant, a variable, or an expression (numeric or character).
format.
is any SAS format or user-defined format that determines these details:
  • the length of the resulting string
  • whether the string is right- or left-aligned
source and format.
must be the same type (numeric or character).

Example: Using the PUT Function

Suppose you want to create a report that shows the amount of fees that are unpaid for a specific course. In the following example, you use the SYMPUTX routine to format the value of the numeric variable Begin_date with the MMDDYY10. format and assign that value to the macro variable Date. Then you also use another call to the SYMPUTX routine to format the result of an expression involving Fee, Total, and Paidup as a dollar amount and assign that value to the macro variable Due.
%let crsnum=3;
data revenue;
   set certadv.all end=final;
   where course_number=&crsnum;
   total+1;
   if paid='Y' then paidup+1;
   if final then do;
     call symputx('crsname',trim(course_title));
     call symputx('date',put(begin_date,mmddyy10.));
     call symputx('due',strip(put(fee*(total-paidup),dollar8.)));
   end;
run;
You can use the macro variables Date and Due in a PROC PRINT step to create your report. The values of these macro variables appear in the report with the formatting that you assigned to them when you created them.
proc print data=revenue;
   var student_name student_company paid;
   title "Fee Status for &crsname (#&crsnum) Held &date";
   footnote "Note: &due in Unpaid Fees";
run;
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
18.191.223.123