SAS 宏函数取决于宏变量的值 [英] SAS Macro function conditional on value of Macro Variable

查看:35
本文介绍了SAS 宏函数取决于宏变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SAS 项目 (EGv7.1),它允许用户在第一行指定一个值.然后,根据指定的值调用其他进程.其中之一是分配了一些其他宏变量.以下是我所拥有的,它似乎不起作用.我真的需要 let 语句在序列中排在第一位,但除此之外,我愿意接受更改.有什么建议吗?

I have a SAS project (EGv7.1) that allows the user to specify a value on the first line. Then, other processes are invoked based on the value specified. One of these is that some other macro variables are assigned. Below is what I have, and it does not seem to be working. I really need the let statement to be first in the sequence, but besides that I am open to changes. Any suggestions?

%let number=8;

%macro my_function();
    %if &number=8 %then 
        %do;
            %let number_text=eight;
            %let number_text_2=equal to eight;
        %end;
    %if &number>8 %then
            %do;
            %let number_text=not eight;
            %let number_text_2=greater then eight;
        %end;
    %if &number<8 %then
            %do;
            %let number_text=not eight;
            %let number_text_2=less than eight;
        %end;
%mend my_function;
%my_function();

%put =================&number==================;
%put ===========The number is &number_text.=============;
%put =======Furthermore, the number is &number_text_2.========;

推荐答案

当您在宏内使用 %let 语句时,变量默认为局部作用域.也就是说,它们只存在于宏内部.为了解决这个问题,在宏中添加一个 %global 语句.

When you use %let statements inside of a macro, the variables default to local scope. That is, they only exist inside the macro. To remedy that add a %global statement inside the macro.

%let number = 8;

%macro my_function();
    %global number_text number_text_2;

    %if %sysevalf(&number = 8) %then 
        %do;
            %let number_text = eight;
            %let number_text_2 = equal to eight;
        %end;
    %else %if %sysevalf(&number > 8) %then
        %do;
            %let number_text = not eight;
            %let number_text_2 = greater than eight;
        %end;
    %else %if %sysevalf(&number < 8) %then
        %do;
            %let number_text = not eight;
            %let number_text_2 = less than eight;
        %end;

%mend my_function;
%my_function();

这告诉 SAS 宏变量 number_textnumber_text_2 应该可以在宏之外访问,这应该可以解决您的问题.

This tells SAS that the macro variables number_text and number_text_2 should be accessible outside of the macro, which should fix your problem.

我还建议将 %else 添加到您的 %if 中.这确保了每个条件只在它前面的条件为假时才被评估.如果没有 %else,则每次都会评估每个条件.

I also recommend adding %else to your %ifs. This ensures that each condition is only evaluated if the one preceding it is false. Without %else, each condition is evaluated every time.

正如@DomPazz 提到的,在评估数字条件时使用 %sysevalf() 是个好主意.

As @DomPazz mentioned, it's a good idea to use %sysevalf() when evaluating numeric conditions.

这篇关于SAS 宏函数取决于宏变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆