如何在SAS中计算出我的宏变量的数据类型

she*_*ode 3 sas sas-macro

如何在日志中打印出宏变量的数据类型

%macro mymacro(dt2);


    %LET c_mth = %SYSFUNC(intnx(month,&dt2.d,-1,e),date9.) ;
    %put &c_mth;

%mend;

mymacro('01sep2014')
Run Code Online (Sandbox Code Playgroud)

我有一堆使用%let或into分配的宏变量:我的问题是我正在尝试在日期上做一堆布尔条件,但我怀疑我的一些变量是字符串,有些是我将它们输入的日期我的代码,但三重检查肯定有办法将一些东西返回到日志

我想要类似于在R中使用str()或mode()或is.numeric()

Que*_*tin 8

H,

SAS宏语言很奇怪.:)

正如Reeza所说,宏变量没有类型,它们都是文本.

但是,如果使用布尔逻辑(%IF语句),并且两个操作数都是整数,则宏语言将进行数字比较而不是字符比较.

因此,您可以使用INPUTN()函数将日期字符串转换为SAS日期(自01Jan1960以来的天数),然后进行比较.这是一个例子,跳出你的代码:

%macro mymacro(dt1,dt2);
  %local c_mth1 c_mth2 n_mth1 n_mth2;

  %let c_mth1 = %sysfunc(intnx(month,&dt1.d,-1,e),date9.) ;
  %let c_mth2 = %sysfunc(intnx(month,&dt2.d,-1,e),date9.) ;
  %let n_mth1 = %sysfunc(inputn(&c_mth1,date9.)) ;
  %let n_mth2 = %sysfunc(inputn(&c_mth2,date9.)) ;

  %put &c_mth1 -- &n_mth1;
  %put &c_mth2 -- &n_mth2;

  %if &n_mth1<&n_mth2 %then %put &c_mth1 is before &c_mth2;
  %else %put &c_mth1 is NOT before &c_mth2;

%mend;
Run Code Online (Sandbox Code Playgroud)

从示例通话中记录:

236  %mymacro('01feb1960','01mar1960')
31JAN1960 -- 30
29FEB1960 -- 59
31JAN1960 is before 29FEB1960
Run Code Online (Sandbox Code Playgroud)

--Q.


Ree*_*eza 5

宏变量没有类型,它们都是文本.

您必须确保以对程序有意义的方式传递变量并生成有效的SAS代码.

%let date1=01Jan2014;
%let date2=31Jan2014;

data _null_;

x = "&date1"d > "&date2"d;
y = "&date2"d > "&date1"d;
z = "&date2"d-"&date1"d;

put 'x=' x;
put 'y=' y;
put 'z=' z;

run;
Run Code Online (Sandbox Code Playgroud)

日志应显示:x = 0 y = 1 z = 30