You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SAS全局宏变量的类型及多方式创建的全局宏变量能否调用咨询

Hey there! Great questions about SAS global macro variables—let’s break these down clearly so you have a solid grasp.

1. Types of Global Macro Variables in SAS

SAS has two main categories of global macro variables:

  • Automatic Global Macro Variables: These are pre-built into SAS and are available as soon as you start your session. They store system-level details like the current SAS version (&SYSVER), today’s date (&SYSDATE), your operating system (&SYSSCP), and even the path to your SAS configuration files. You don’t need to define them—they’re ready to use out of the box.

  • User-Defined Global Macro Variables: These are variables you create to store custom values. You can make them global in two ways:

    • Define them directly in your main SAS code (outside any macro definition), or
    • Explicitly declare them as global inside a macro using the %GLOBAL statement before assigning a value.
2. Can You Call Global Macro Variables Created via Different Methods?

Absolutely! Any global macro variable—regardless of how you created it—is accessible throughout your entire SAS session (in main code and inside any macros, as long as there’s no local variable with the same name overriding it). Let’s walk through each method you mentioned:

  • %LET:

    • If you use %LET outside a macro, the variable is global by default.
    • If you use it inside a macro without declaring %LOCAL, it becomes global if no local variable with that name exists.
      Either way, once it’s global, you can reference it anywhere. Example:
    %let global_let = Hello from %LET; /* Global by default */
    %macro test_let;
      %put &global_let; /* Works inside the macro */
    %mend test_let;
    %test_let;
    %put &global_let; /* Works in main code too */
    
  • %DO (Macro Loops):
    The %DO statement itself doesn’t create macro variables, but if you’re generating variables inside a loop (like %let loop_var&i = ...), you can make them global by either declaring them with %GLOBAL first or defining them outside the macro. Once global, they’re accessible everywhere. Example:

    %global loop_var1 loop_var2;
    %macro loop_test;
      %do i=1 %to 2;
        %let loop_var&i = Loop value &i;
      %end;
    %mend loop_test;
    %loop_test;
    %put &loop_var1 &loop_var2; /* Both variables are accessible globally */
    
  • CALL SYMPUT/CALL SYMPUTX:
    These DATA step routines create macro variables.

    • If used in the main DATA step (not inside a macro), the variable is global by default.
    • If used inside a macro, you can explicitly make it global by either declaring it with %GLOBAL before the DATA step or using the G option in CALL SYMPUTX (available in SAS 9.4 and later).
      Once global, you can call it anywhere. Example:
    data _null_;
      call symputx('symput_global', 'Created via SYMPUTX', 'G'); /* Explicitly mark as global */
    run;
    %put &symput_global; /* Works in main code */
    %macro test_symput;
      %put &symput_global; /* Works inside the macro */
    %mend test_symput;
    %test_symput;
    
  • SQL INTO Clause:
    When you use SELECT ... INTO :macro_var FROM ... in a PROC SQL step outside a macro, the macro variable is global by default. If you run this inside a macro, declare the variable with %GLOBAL first to ensure it’s accessible outside the macro. Example:

    proc sql noprint;
      select count(*) into :sql_global from sashelp.class;
    quit;
    %put Total students in sashelp.class: &sql_global; /* Accessible globally */
    
  • Macro Parameters:
    Macro parameters are local to the macro by default, but you can easily make their values global by assigning the parameter to a pre-declared global variable inside the macro. Alternatively, if you pass a global variable as an argument to the macro, the macro can modify it (unless the parameter is declared local, which overrides the global variable). Example:

    %global param_global;
    %macro test_param(input_val);
      %let param_global = &input_val; /* Assign parameter value to global variable */
    %mend test_param;
    %test_param(Hello from macro parameter);
    %put &param_global; /* Accessible globally */
    

内容的提问来源于stack exchange,提问作者krishna reddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:42:16