Hi
vtype() would return the datatype, whether the variable is character or numeric.
Is there a function to know the macrovariable data type?
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.
@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()
%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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.