Alphabetical listing of Global macro variables

From sasCommunity
Jump to: navigation, search

Question: How can I get an alphabetical list of global macro variables?

Illustration

**** allocate list of macro variable;
%Let a1 = a1;
%Let z9 = z999;
%Let b2 = *b*;
 
**** write values to log;
%Put _User_;
11   %Put _User_;
GLOBAL Z9 z999
GLOBAL A1 a1
GLOBAL B2 *b*

Solution

Use dictionary.macros to create a list of %put mvar: &Mvar; statements.

Here is the description of the dictionary table:

PROC SQL; describe table dictionary.macros;
          quit;
NOTE: SQL table DICTIONARY.MACROS was created like:

create table DICTIONARY.MACROS
  (
   scope char(32) label='Macro Scope',
   name char(32) label='Macro Variable Name',
   offset num label='Offset into Macro Variable',
   value char(200) label='Macro Variable Value'
  );

Utility Put-User

This is the AutoMagic select statement that produces the list of %Put statements.

The trick is the order by name phrase.

 /*    name: <UNC>\SAS-site\includes\put-user.sas
description: write alphabetical list of global macro variables to log
    purpose: ease of reading log
usage:
*in autoexec:;
filename SiteIncl '<UNC>\SAS-site\includes';
*in program:;
%Include SiteIncl(put-user);
RJF2 2012-Apr-25
http://www.sascommunity.org/wiki/
Alphabetical_listing_of_Global_macro_variables
PROC SQL; describe table Dictionary.Macros;
          quit;
create table DICTIONARY.MACROS
   scope  char(32)  label='Macro Scope',
   name   char(32)  label='Macro Variable Name',
   value  char(200) label='Macro Variable Value'          
this    :  !! ': &' !! trim(Name)  !! ';'
could be:  !! ': '  !! trim(Value) !! ';'
***********/  
 
PROC SQL noprint; 
         select   '%Put global: ' 
                !! trim(Name) !! ': &'  
                !! trim(Name) !! ';'
         into  :SQL_List  separated by ' '
         from  Dictionary.Macros
         where Scope eq 'GLOBAL'
           and substr(Name,1,3) not in('SYS', 'SQL')
         order by Name;
         &SQL_List.
         quit;
*house-cleaning;
%symdel SQL_List;

The macro variable SQL_List contains these statements:

  • %Put A1: &A1;
  • %Put B2: &B2;
  • %Put Z9: &Z9;

and the log has:

A1: a1
B2: *b*
Z9: z999


Usage in AutoExec

**** allocate list of macro variable;
%Let Software = SAS;
%Let DateTxt  = 2012-05-10;
%Let DateSas  = %sysfunc(translate(&DateTxt.,_,-));
 
filename SiteIncl '<UNC>\SAS-site\includes';
%Include SiteIncl(put-global);

and the log has:

global: DATESAS: 2012_05_10
global: DATETXT: 2012-05-10
global: SOFTWARE: SAS

Code for Development

This program illustrates my development discipline.

Note: these are slides for 2012-May-16 presentation at Greater Atlanta SAS Users Group.

SQL

  1. describe table
  2. select _all_
  3. select unique
  4. select subset
  5. report

in general:

  1. review data structure
  2. look at data
  3. review cardinality ratio to identify foreign keys
  4. identify subset
  5. report
PROC SQL; describe table Dictionary.Macros; 
 
          select * from  Dictionary.Macros;
 
          select Scope, Name, Value from  Dictionary.Macros;
 
          select Name, Value 
            from  Dictionary.Macros
           where Scope eq 'GLOBAL'
             and substr(Name,1,3) not in('SYS', 'SQL');
 
          select Name, Value 
            from  Dictionary.Macros
           where Scope eq 'GLOBAL'
             and substr(Name,1,3) not in('SYS', 'SQL')
           order by Name;
 
         select '%Put global: ' !! trim(Name) 
               !! ': ' !! trim(Value) !! ';'
         from  Dictionary.Macros
         where Scope eq 'GLOBAL'
           and substr(Name,1,3) not in('SYS', 'SQL')
         order by Name;
         quit;

References

--Ronald_J._Fehd macro.maven == the radical programmer 08:55, 25 April 2012 (EDT)