Home Forums Main Forums SAS Forum Local or global? It is a question!

  • Local or global? It is a question!

     Datura updated 3 years, 1 month ago 1 Member · 2 Posts
  • Datura

    Member
    February 1, 2021 at 11:38 am

    Macro language is the advanced programming in SAS. It sounds hard and confusing for most beginners. Please look at below example, why the macro variable Proc is global rather than local ???

    %macro one;
    data _null_;
    A=333;
    call symput("proc", "Means");
    run;

    %let zzz=&proc;
    run;
    %put _user_;
    %mend;
    %one;

    However, the macro variable Proc is local in the below codes:

    %macro one (test);
    data _null_;
    A=333;
    call symput("proc", "Means");
    run;
    %let zzz=&proc;
    run;
    %put _user_;
    %mend;
    %one (Maths);

    Isn’t it incredible? What makes the difference? The key point is: When SAS creates macro variables by using Call Symput or SQL Select Into, the created macro variables will be stored in the most local, non-empty symbol tables. This will determine if a macro variable is local or global. Give below code:

    %macro one;
    %let Test=English;
    data _null_;
    A=333;
    call symput("proc", "Means");
    run;
    %put _user_;
    %mend;
    %one;

    The macro variable Proc is local because the macro %one is non-empty when it is created. However, if we delete the %let Test=English, or place it after the CALL SYMPUT statement, then it will create the macro variable Proc as Global.

    Interesting? Crazy? Test it in SAS by yourself!

  • Datura

    Member
    February 1, 2021 at 11:49 am

    Call SYMPUT rountine

    Call SYMPUT is a call routine in SAS, it generate a macro variable in a DATA step and puts the macro variable in the most local non-empty symbol table. A symbol table is nonempty if it contains the following:
    1. a value;
    2. a computed %GOTO (A computed %GOTO contains % or & and resolves to a
    label.)
    3. the macro variable &SYSPBUFF, created at macro invocation time.

    CALL SYMPUTX Routine: It is an upgraded version of Call Symput with below enhanced features.
    1. Assigns a value to a macro variable, and removes both leading and trailing
    blanks.

    2. It has the Symbol Table argument which can be used to specify a macro variable to be local or global.

     Call SymputX ("City", "Chicago",  "G/L/F");

    Symbol-table: G, L, F.

    It 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, which will be the global symbol table, if 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.

    Note: If you omit symbol-table, or if symbol-table is blank, CALL SYMPUTX stores the macro variable in the same symbol table as does the CALL SYMPUT routine.

    Attributed to these enhanced features, therefore I strongly recommend to use Call SymputX rather than Call Symput in work.

    • This reply was modified 3 years, 1 month ago by  Datura.

Log in to reply.

Original Post
0 of 0 posts June 2018
Now