<?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 Re: Capture Date Format in a Macro Variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Capture-Date-Format-in-a-Macro-Variable/m-p/771700#M244941</link>
    <description>And SASHELP.VCOLUMN has the formats that can be queried or VFORMAT will return a format of a variable within the data step. &lt;BR /&gt;&lt;BR /&gt;VFORMAT()&lt;BR /&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0gph5udn7gkvdn1cnc641x4212a.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0gph5udn7gkvdn1cnc641x4212a.htm&lt;/A&gt;</description>
    <pubDate>Fri, 01 Oct 2021 20:50:38 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-10-01T20:50:38Z</dc:date>
    <item>
      <title>Capture Date Format in a Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capture-Date-Format-in-a-Macro-Variable/m-p/771696#M244938</link>
      <description>&lt;P&gt;I have a data set with numerous SAS date columns, and each date column has a different SAS date format.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking for an easier way to extract the desired date format and convert into a macro variable to be referenced later in the program.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there an easier way to extract the date format without having to create and delete a contents dataset?&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input units date1 :mmddyy10. date2 :yymmdd10. date3 :yyq6.;
format date1 mmddyy10. date2 yymmdd10. date3 yyq6.;
datalines;
2 01/04/2014 20140104 2014Q1
6 01/05/2014 20140105 2014Q1
7 01/06/2014 20140106 2014Q1
;
run;


proc contents data=have out=have_contents noprint;
run;

data _null_;
 set have_contents;
  if NAME='date3' then call symputx('want',strip(compress(FORMAT||put(FORMATL,2.)||'.')),'g');
run;

proc delete data=have_contents; run;

%put date format = &amp;amp;want;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know if it exists, but envisioning something like a metadata function that can be referenced like: %let want = %sysfunc(datefmt(ds=have,col=date3));&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Oct 2021 20:34:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capture-Date-Format-in-a-Macro-Variable/m-p/771696#M244938</guid>
      <dc:creator>rah1992</dc:creator>
      <dc:date>2021-10-01T20:34:45Z</dc:date>
    </item>
    <item>
      <title>Re: Capture Date Format in a Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capture-Date-Format-in-a-Macro-Variable/m-p/771699#M244940</link>
      <description>Why? Date calculations and references don't typically rely on the specific date format so if you store the data the format that's fine and usually more efficient for further usages.</description>
      <pubDate>Fri, 01 Oct 2021 20:46:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capture-Date-Format-in-a-Macro-Variable/m-p/771699#M244940</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-01T20:46:36Z</dc:date>
    </item>
    <item>
      <title>Re: Capture Date Format in a Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capture-Date-Format-in-a-Macro-Variable/m-p/771700#M244941</link>
      <description>And SASHELP.VCOLUMN has the formats that can be queried or VFORMAT will return a format of a variable within the data step. &lt;BR /&gt;&lt;BR /&gt;VFORMAT()&lt;BR /&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0gph5udn7gkvdn1cnc641x4212a.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0gph5udn7gkvdn1cnc641x4212a.htm&lt;/A&gt;</description>
      <pubDate>Fri, 01 Oct 2021 20:50:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capture-Date-Format-in-a-Macro-Variable/m-p/771700#M244941</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-01T20:50:38Z</dc:date>
    </item>
    <item>
      <title>Re: Capture Date Format in a Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capture-Date-Format-in-a-Macro-Variable/m-p/771703#M244944</link>
      <description>&lt;P&gt;To rephrase (and perhaps augment) the correct advice from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DO NOT FORMAT MACRO VARIABLES* for Boolean or arithmetic uses. Leave them unformatted, and your code works easily. Formatting them is just extra work, and then you have to either un-format them or start parsing text strings. SAS does not need formatted macro variables for this purpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;* — the exception&amp;nbsp;(which does not apply to Boolean or arithmetic uses) is when the macro variable needs to be used in human readable form, such as in a title or label or file name. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Oct 2021 21:07:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capture-Date-Format-in-a-Macro-Variable/m-p/771703#M244944</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-10-01T21:07:42Z</dc:date>
    </item>
    <item>
      <title>Re: Capture Date Format in a Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capture-Date-Format-in-a-Macro-Variable/m-p/771705#M244946</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/401167"&gt;@rah1992&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Combining&amp;nbsp;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879" target="_blank" rel="noopener"&gt;Reeza&lt;/A&gt;'s suggestion (SASHELP.VCOLUMN) with the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/ds2ref/n11e05vmcxh3qvn1g1scs7lfe4bc.htm" target="_blank" rel="noopener"&gt;FMTINFO function&lt;/A&gt; you can even select the date variables without knowing their names:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set sashelp.vcolumn(where=(libname='WORK' &amp;amp; memname='HAVE' &amp;amp; format));
if fmtinfo(substr(format,1,anyfirst(format,-99)),'cat')='date';
call symputx(name,format);
run;

%put _user_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result for your sample data:&lt;/P&gt;
&lt;PRE&gt;GLOBAL DATE1 MMDDYY10.
GLOBAL DATE2 YYMMDD10.
GLOBAL DATE3 YYQ6.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Oct 2021 21:47:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capture-Date-Format-in-a-Macro-Variable/m-p/771705#M244946</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-10-01T21:47:18Z</dc:date>
    </item>
    <item>
      <title>Re: Capture Date Format in a Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capture-Date-Format-in-a-Macro-Variable/m-p/771734#M244956</link>
      <description>&lt;P&gt;Thanks,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;. It works great!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found a&amp;nbsp;&lt;A href="https://support.sas.com/resources/papers/proceedings17/0835-2017.pdf" target="_self"&gt;global forum paper&lt;/A&gt;&amp;nbsp;showing yet another way for capturing metadata without using proc contents. Both are improvements to the original. Thanks again.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro varexist(dsn=,varname=);
 %local dsid vnum;
  %let vnum=0;
  %let dsid = %sysfunc(open(&amp;amp;dsn));
   %if &amp;amp;dsid %then %do;
    %let vnum = %sysfunc(varnum(&amp;amp;dsid,&amp;amp;varname));
    %let dsid = %sysfunc(close(&amp;amp;dsid));
   %end;
 &amp;amp;vnum
%mend varexist;

/* Check for existence of timeid in dsn, and assign same date format as dsn and timeid provided in macro */
%macro dateformat(dsn=,timeid=);
 %local dsid;
  %let dsid= %sysfunc(open(&amp;amp;dsn));
    %if %varexist(dsn=&amp;amp;dsn,varname=&amp;amp;timeid) %then %do;
     %let fmt=%sysfunc(varfmt(&amp;amp;dsid,%sysfunc(varnum(&amp;amp;dsid,&amp;amp;timeid))));
     %let dsid= %sysfunc(close(&amp;amp;dsid));
   %end;
 &amp;amp;fmt
%mend;

%put date format = %dateformat(dsn=have,timeid=date1);&lt;BR /&gt;&lt;BR /&gt;data&amp;nbsp;new;&lt;BR /&gt;&amp;nbsp;set&amp;nbsp;sashelp.air;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;format&amp;nbsp;date&amp;nbsp;%dateformat(dsn=have,timeid=date3);&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Oct 2021 04:28:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capture-Date-Format-in-a-Macro-Variable/m-p/771734#M244956</guid>
      <dc:creator>rah1992</dc:creator>
      <dc:date>2021-10-02T04:28:41Z</dc:date>
    </item>
  </channel>
</rss>

