shecode
shecode

Reputation: 1726

How do I work out the data type of my macro variable in SAS

How do I print out the data type of a macro variable in the log

%macro mymacro(dt2);


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

%mend;

mymacro('01sep2014')

I have a bunch of macro variables assigned using a %let or into: my problem is I'm trying to do a bunch of boolean condition on dates but I suspect that some of my variables are strings and some are dates I have casted them in my code but to triple check there is surely a way to return something to the log

I want something similar to using str() or mode() or is.numeric() in R

Upvotes: 3

Views: 1240

Answers (3)

user667489
user667489

Reputation: 9569

If your macro variables resolve to date literals, you can use intck combined with %eval to compare them, e.g.

%let mvar1 = '01jan2015'd;
%let mvar2 = '01feb2015'd;

/*Prints 1 if mvar2 > mvar1*/
%put %eval(%sysfunc(intck(day,&mvar1,&mvar2)) > 0);

Upvotes: 1

Quentin
Quentin

Reputation: 6378

H,

The SAS macro language is weird. : )

As Reeza said, macro variables do not have a type, they are all text.

But, if you use Boolean logic (%IF statement), and both operands are integers, the macro language will do a numeric comparison rather than a character comparison.

So you can use the INPUTN() function to convert the date strings to SAS dates (number of days since 01Jan1960), and then compare those. Here's an example, jumping off from your code:

%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;

Log from a sample call:

236  %mymacro('01feb1960','01mar1960')
31JAN1960 -- 30
29FEB1960 -- 59
31JAN1960 is before 29FEB1960

--Q.

Upvotes: 8

Reeza
Reeza

Reputation: 21264

Macro variables do not have a type, they are all text.

You have to make sure the variable is passed in a way that makes sense to the program and generates valid SAS code.

%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;

Log should show: x=0 y=1 z=30

Upvotes: 5

Related Questions