Previous Page | Next Page

DATA Step Functions for Macros

SYMGET Function



Returns the value of a macro variable to the DATA step during DATA step execution.
Type: DATA step function
See also:

RESOLVE Function

SYMGETN Function

CALL SYMPUT Routine

CALL SYMPUTN Routine


Syntax
Details
Comparisons
Example
Retrieving Variable Values Previously Assigned from a Data Set

Syntax

SYMGET(argument)

argument

can be one of the following items:

  • the name of a macro variable within quotation marks but without an ampersand. When a macro variable value contains another macro variable reference, SYMGET does not attempt to resolve the reference. If argument references a nonexistent macro variable, SYMGET returns a missing value. This example shows how to assign the value of the macro variable G to the DATA step variable X.

    x=symget('g');
  • the name of a DATA step character variable, specified with no quotation marks, which contains names of one or more macro variables. If the value is not a valid SAS name, or if the macro processor cannot find a macro variable of that name, SAS writes a note to the log that the function has an illegal argument and sets the resulting value to missing. For example, these statements assign the value stored in the DATA step variable CODE, which contains a macro variable name, to the DATA step variable KEY:

    length key $ 8;
    input code $;
    key=symget(code);

    Each time the DATA step iterates, the value of CODE supplies the name of a macro variable whose value is then assigned to KEY.

  • a character expression that constructs a macro variable name. For example, this statement assigns the letter s and the number of the current iteration (using the automatic DATA step variable _N_).

    score=symget('s'||left(_n_));

Details

SYMGET returns a character value that is the maximum length of a DATA step character variable. A returned value that is longer is truncated.

If SYMGET cannot locate the macro variable identified as the argument, it returns a missing value, and the program issues a message for an illegal argument to a function.

SYMGET can be used in all SAS language programs, including SCL programs. Because it resolves variables at program execution instead of macro execution, SYMGET should be used to return macro values to DATA step views, SQL views, and SCL programs.


Comparisons


Example


Example 1: Retrieving Variable Values Previously Assigned from a Data Set

data dusty;
   input dept $ name $ salary @@;
   datalines;
bedding Watlee 18000    bedding Ives 16000
bedding Parker 9000     bedding George 8000
bedding Joiner 8000     carpet Keller 20000
carpet Ray 12000        carpet Jones 9000
gifts Johnston 8000     gifts Matthew 19000
kitchen White 8000      kitchen Banks 14000
kitchen Marks 9000      kitchen Cannon 15000
tv Jones 9000           tv Smith 8000
tv Rogers 15000         tv Morse 16000
;

proc means noprint;
   class dept;
   var salary;
   output out=stats sum=s_sal;
run;

proc print data=stats;
   var dept s_sal;
   title "Summary of Salary Information";
   title2 "For Dusty Department Store";
run;

data _null_;
   set stats;
   if _n_=1 then call symput('s_tot',s_sal);
   else call symput('s'||dept,s_sal);
run;

data new;
   set dusty;
   pctdept=(salary/symget('s'||dept))*100;
   pcttot=(salary/&s_tot)*100;
run;

proc print data=new split="*";
   label dept   ="Department"
         name   ="Employee"
         pctdept="Percent of *Department* Salary"
         pcttot ="Percent of *   Store  * Salary";
   format pctdept pcttot 4.1;
   title  "Salary Profiles for Employees";
   title2 "of Dusty Department Store";
run;

This program produces the output shown in the following output.

Intermediate Data Set and Final Report

                       Summary of Salary Information                      1
                        For Dusty Department Store


                         OBS    DEPT        S_SAL

                          1                221000
                          2     bedding     59000
                          3     carpet      41000
                          4     gifts       27000
                          5     kitchen     46000
                          6     tv          48000


                       Salary Profiles for Employees                      2
                          Dusty Department Store


                                         Percent of  Percent of
                                         Department    Store
    OBS  Department  Employee  SALARY    Salary        Salary

      1   bedding    Watlee     18000     30.5           8.1
      2   bedding    Ives       16000     27.1           7.2
      3   bedding    Parker      9000     15.3           4.1
      4   bedding    George      8000     13.6           3.6
      5   bedding    Joiner      8000     13.6           3.6
      6   carpet     Keller     20000     48.8           9.0
      7   carpet     Ray        12000     29.3           5.4
      8   carpet     Jones       9000     22.0           4.1
      9   gifts      Johnston    8000     29.6           3.6
     10   gifts      Matthew    19000     70.4           8.6
     11   kitchen    White       8000     17.4           3.6
     12   kitchen    Banks      14000     30.4           6.3
     13   kitchen    Marks       9000     19.6           4.1
     14   kitchen    Cannon     15000     32.6           6.8
     15   tv         Jones       9000     18.8           4.1
     16   tv         Smith       8000     16.7           3.6
     17   tv         Rogers     15000     31.3           6.8
     18   tv         Morse      16000     33.3           7.2

Previous Page | Next Page | Top of Page