<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic function to derive macrovariable datatype in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/function-to-derive-macrovariable-datatype/m-p/897701#M354777</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;vtype() would return the datatype, whether the variable is character or numeric.&lt;/P&gt;&lt;P&gt;Is there a function to know the macrovariable data type?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 08 Oct 2023 02:51:37 GMT</pubDate>
    <dc:creator>kkk9</dc:creator>
    <dc:date>2023-10-08T02:51:37Z</dc:date>
    <item>
      <title>function to derive macrovariable datatype</title>
      <link>https://communities.sas.com/t5/SAS-Programming/function-to-derive-macrovariable-datatype/m-p/897701#M354777</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;vtype() would return the datatype, whether the variable is character or numeric.&lt;/P&gt;&lt;P&gt;Is there a function to know the macrovariable data type?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Oct 2023 02:51:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/function-to-derive-macrovariable-datatype/m-p/897701#M354777</guid>
      <dc:creator>kkk9</dc:creator>
      <dc:date>2023-10-08T02:51:37Z</dc:date>
    </item>
    <item>
      <title>Re: function to derive macrovariable datatype</title>
      <link>https://communities.sas.com/t5/SAS-Programming/function-to-derive-macrovariable-datatype/m-p/897702#M354778</link>
      <description>&lt;P&gt;SAS macro variables are always string variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is autocall macro &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p14qy9r4wu1an0n11kfn30idvy20.htm" target="_self"&gt;%datatyp()&lt;/A&gt;&amp;nbsp;that allows you to check if the value (string) stored in the macro variable could get assigned to a SAS numerical variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Oct 2023 03:01:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/function-to-derive-macrovariable-datatype/m-p/897702#M354778</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-08T03:01:39Z</dc:date>
    </item>
    <item>
      <title>Re: function to derive macrovariable datatype</title>
      <link>https://communities.sas.com/t5/SAS-Programming/function-to-derive-macrovariable-datatype/m-p/897703#M354779</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/454447"&gt;@kkk9&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;vtype() would return the datatype, whether the variable is character or numeric.&lt;/P&gt;
&lt;P&gt;Is there a function to know the macrovariable data type?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;MACRO variables do NOT have a "data type".&amp;nbsp; Every macro variable just holds TEXT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is a ANCIENT SAS supplied autocall macro that will GUESS whether the string in the macro variable could represent a number.&amp;nbsp; %DATATYP()&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%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(&amp;amp;parm));
   %let len = %length(&amp;amp;parm);
   %if &amp;amp;len &amp;gt; 0 %then %do;
      %if %verify(&amp;amp;parm,%str(0123456789+-.EeDd))=0 %then %do;
         %let char = %qsubstr(&amp;amp;parm,1,1);
         %if &amp;amp;char=%str(+) | &amp;amp;char=%str(-) %then %do;
            %if &amp;amp;len &amp;lt; 2 %then %let parm = ;
            %else %let parm = %qsubstr(&amp;amp;parm,2);
            %let len = %eval(&amp;amp;len - 1);
            %end;
         %let fract = 0;
         %let expon = 0;
%repeat: %if &amp;amp;len &amp;gt; 0 %then %do;
            %let pos = %verify(&amp;amp;parm,0123456789);
            %if &amp;amp;pos = 0 %then %let type=NUMERIC;
            %else %do;
               %if &amp;amp;len &amp;gt; &amp;amp;pos %then %let parm = %qsubstr(&amp;amp;parm,&amp;amp;pos);
               %let len = %length(&amp;amp;parm);
               %let char = %qsubstr(&amp;amp;parm,1,1);
               %if &amp;amp;char=%str(.) &amp;amp; &amp;amp;fract=0 &amp;amp; &amp;amp;expon=0 %then %do;
                  %if &amp;amp;len &amp;lt; 2 %then %let parm = ;
                  %else %let parm = %qsubstr(&amp;amp;parm,2);
                  %let len = %eval(&amp;amp;len - 1);
                  %let fract = 1;
                  %goto repeat;
                  %end;
               %else %if (&amp;amp;char=%str(E) | &amp;amp;char=%str(e) |
                          &amp;amp;char=%str(D) | &amp;amp;char=%str(d)) &amp;amp;
                         &amp;amp;expon=0 &amp;amp; &amp;amp;pos&amp;gt;1 &amp;amp; &amp;amp;len&amp;gt;1 %then %do;
                  %let parm = %qsubstr(&amp;amp;parm,2);
                  %let len = %eval(&amp;amp;len - 1);
                  %let char = %qsubstr(&amp;amp;parm,1,1);
                  %if &amp;amp;char=%str(+) | &amp;amp;char=%str(-) %then %do;
                     %if &amp;amp;len &amp;lt; 2 %then %let parm = ;
                     %else %let parm = %qsubstr(&amp;amp;parm,2);
                     %let len = %eval(&amp;amp;len - 1);
                     %end;
                  %let expon = 1;
                  %goto repeat;
                  %end;
               %end;
            %end;
         %end;
      %end;
  &amp;amp;type
%mend datatyp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;But you could probably write something much simpler, perhaps using Regular Expression using a MODERN version of SAS.&lt;/P&gt;</description>
      <pubDate>Sun, 08 Oct 2023 03:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/function-to-derive-macrovariable-datatype/m-p/897703#M354779</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-10-08T03:13:19Z</dc:date>
    </item>
  </channel>
</rss>

