BookmarkSubscribeRSS Feed
kkk9
Calcite | Level 5

Hi 

 

vtype() would return the datatype, whether the variable is character or numeric.

Is there a function to know the macrovariable data type? 

 

2 REPLIES 2
Patrick
Opal | Level 21

SAS macro variables are always string variables. 

There is autocall macro %datatyp() that allows you to check if the value (string) stored in the macro variable could get assigned to a SAS numerical variable.

 

Tom
Super User Tom
Super User

@kkk9 wrote:

Hi 

 

vtype() would return the datatype, whether the variable is character or numeric.

Is there a function to know the macrovariable data type? 

 


MACRO variables do NOT have a "data type".  Every macro variable just holds TEXT.

 

There is a ANCIENT SAS supplied autocall macro that will GUESS whether the string in the macro variable could represent a number.  %DATATYP()

Spoiler
%macro datatyp(parm);
%*********************************************************************;
%*                                                                   *;
%*  MACRO: DATATYP                                                   *;
%*                                                                   *;
%*  USAGE: %datatyp(parm)                                            *;
%*                                                                   *;
%*  DESCRIPTION:                                                     *;
%*    The DATATYP macro determines if the input parameter is         *;
%*    NUMERIC or CHARacter data, and returns either CHAR or NUMERIC  *;
%*    depending on the value passed in through parm.                 *;
%*                                                                   *;
%*  PROCEDURE:                                                       *;
%*    This macro checks first removes leading and trailing blanks.   *;
%*    Then it checks for and removes a sign character (+ or -).      *;
%*    Then it checks for a string of digits, a decimal point, or     *;
%*    a floating point exponent, followed by an optional sign and    *;
%*    a string of digits.                                            *;
%*                                                                   *;
%*  ERRORS/RESTRICTIONS:                                             *;
%*    This macro requires the %VERIFY macro.  Commas are not         *;
%*    allowed as the SAS word scanner does not accept them.  It      *;
%*    is not guaranteed that a string judged NUMERIC will in fact    *;
%*    be acceptable to the SAS word scanner as no range checking     *;
%*    is done.                                                       *;
%*                                                                   *;
%*********************************************************************;
   %local type char len pos fract expon;
   %let type=CHAR;
   %let parm=%qleft(%qtrim(&parm));
   %let len = %length(&parm);
   %if &len > 0 %then %do;
      %if %verify(&parm,%str(0123456789+-.EeDd))=0 %then %do;
         %let char = %qsubstr(&parm,1,1);
         %if &char=%str(+) | &char=%str(-) %then %do;
            %if &len < 2 %then %let parm = ;
            %else %let parm = %qsubstr(&parm,2);
            %let len = %eval(&len - 1);
            %end;
         %let fract = 0;
         %let expon = 0;
%repeat: %if &len > 0 %then %do;
            %let pos = %verify(&parm,0123456789);
            %if &pos = 0 %then %let type=NUMERIC;
            %else %do;
               %if &len > &pos %then %let parm = %qsubstr(&parm,&pos);
               %let len = %length(&parm);
               %let char = %qsubstr(&parm,1,1);
               %if &char=%str(.) & &fract=0 & &expon=0 %then %do;
                  %if &len < 2 %then %let parm = ;
                  %else %let parm = %qsubstr(&parm,2);
                  %let len = %eval(&len - 1);
                  %let fract = 1;
                  %goto repeat;
                  %end;
               %else %if (&char=%str(E) | &char=%str(e) |
                          &char=%str(D) | &char=%str(d)) &
                         &expon=0 & &pos>1 & &len>1 %then %do;
                  %let parm = %qsubstr(&parm,2);
                  %let len = %eval(&len - 1);
                  %let char = %qsubstr(&parm,1,1);
                  %if &char=%str(+) | &char=%str(-) %then %do;
                     %if &len < 2 %then %let parm = ;
                     %else %let parm = %qsubstr(&parm,2);
                     %let len = %eval(&len - 1);
                     %end;
                  %let expon = 1;
                  %goto repeat;
                  %end;
               %end;
            %end;
         %end;
      %end;
  &type
%mend datatyp;

But you could probably write something much simpler, perhaps using Regular Expression using a MODERN version of SAS.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 769 views
  • 0 likes
  • 3 in conversation