SAS中此%宏函数的输出将是什么以及如何在SAS Studio中显示输出

发布于 2025-01-20 21:42:37 字数 224 浏览 2 评论 0原文

%macro segm1;
  data _null_;
    %do i=0 %to 8;
      call symput("yyyymm_i",put(intnx('month',today(),-1,'b'),yymmn6.));
    %end;
%mend;
%segm1;
run;

SAS工作室中此宏代码的输出以及如何获取/dispaly/tive输出将是什么?

%macro segm1;
  data _null_;
    %do i=0 %to 8;
      call symput("yyyymm_i",put(intnx('month',today(),-1,'b'),yymmn6.));
    %end;
%mend;
%segm1;
run;

What will be the output and how to get/dispaly/view output of this macro code in sas studio?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坚持沉默 2025-01-27 21:42:37

由于宏用于生成SAS代码,以在运行之前查看MAPRINT选项的输出。您将看到宏生成数据语句和8个调用Symput语句。

该代码有很多问题。

  1. 使用%do循环在应该使用DO循环的地方。
  2. 一遍又一遍地创建相同的宏变量。
  3. 启动数据步骤,但没有结束。这是故意的吗?为什么?
  4. 创建局部宏变量,当宏完成时将消失。
  5. 使用较旧的功能较低的呼叫Symput()函数,而不是Call Symputx()。

如果要创建8个宏变量,只需使用普通的DO循环即可。无需宏。使用循环变量的值更改生成的宏变量的名称以及结果表示的月份。

data _null_;
  do i=0 to 8;
    call symputx(cats('yyyymm_',i),put(intnx('month',today(),-i,'b'),yymmn6.));
  end;
run;

它将创建一系列名为yyyymm_0到yyyymm_8的宏变量,并用六位数字字符串(如202204,202203),...代表当前月至八个月前,

如果您想在宏中运行该宏并具有宏变量,则它会创建它。在宏结束后可用,然后将可选的第三个参数设置为将Symputx()调用到字符串“ G”,以便将它们定义在全局符号表中而不是本地符号表中。

Since a macro is used to generate SAS code to view the output of macro set the MPRINT option before running it. You will see that the macro generates a data statement and 8 call symput statements.

There are a lot of problems with that code.

  1. Uses %DO loop where it should be using a DO loop.
  2. Creates the same macro variable over and over.
  3. Starts a data step, but does not end it. Was this on purpose? Why?
  4. Creates LOCAL macro variables that will disappear when the macro finishes.
  5. Uses older less functional call symput() function instead of call symputx().

If you want to create 8 macro variables just use a normal DO loop. No need for a macro. Use the value of the loop variable to change the name of the macro variable generated and the month the result represents.

data _null_;
  do i=0 to 8;
    call symputx(cats('yyyymm_',i),put(intnx('month',today(),-i,'b'),yymmn6.));
  end;
run;

Which will create a series of macro variables named YYYYMM_0 to YYYYMM_8 with six digit strings like 202204 , 202203 , ... representing the current month back to eight months ago

If you did want to run that inside a macro and have the macro variables it creates available after the macro ends then set the optional third parameter to call symputx() to the string 'G' so that they are defined in the global symbol table instead of the local symbol table.

尽揽少女心 2025-01-27 21:42:37

您可以使用%put语句将单个宏变量打印到日志中,例如%put& yyyymm_i;

您可以使用>%put _all_;;,或者,如果您仅对自己创建的变量感兴趣:%put _user _;(或在宏%内部put put put _local _;

)代码是错误的,请尝试

%macro segm1;
  data _null_;
    %do i=0 %to 8;
      call symput("yyyymm_&i", put(intnx('month',today(),-1,'b'),yymmn6.));
    %end;
%mend;
%segm1;
run;

%put _user_;

You can print individual macro variables to the log with the %put statement, like %put &yyyymm_i;

You can print all macro variable with %put _all_;, or if you are only interested in variables you created yourself: %put _user_; (or within a macro %put _local_;)

By the way, your code is wrong, try this

%macro segm1;
  data _null_;
    %do i=0 %to 8;
      call symput("yyyymm_&i", put(intnx('month',today(),-1,'b'),yymmn6.));
    %end;
%mend;
%segm1;
run;

%put _user_;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文