<?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: help with macro to convert date from gmt to cst in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/help-with-macro-to-convert-date-from-gmt-to-cst/m-p/633921#M188112</link>
    <description>I had the right idea but poor execution. Thanks so much Patrick for the solution and quick response.</description>
    <pubDate>Sun, 22 Mar 2020 05:56:41 GMT</pubDate>
    <dc:creator>DanD999</dc:creator>
    <dc:date>2020-03-22T05:56:41Z</dc:date>
    <item>
      <title>help with macro to convert date from gmt to cst</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-macro-to-convert-date-from-gmt-to-cst/m-p/633918#M188109</link>
      <description>&lt;P&gt;The macro I'm using was written by someone else. It's been in use for years so I didn't anticipate any problems but when a date is missing it creates a date of 12/31/1959 and a time of 19:00. At first I put an if statement in my data step and that didn't work so I tried putting it in the macro but that didn't work either.&lt;/P&gt;
&lt;P&gt;Original macro:&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro make_cst_from_gmt(dttm);
	/* Change the date to CST from GMT */
	/* nwkdom(2, 1, 3, year(datepart(&amp;amp;dttm))); */ /* DST begins 2nd Sun in March */
	/* nwkdom(1, 1,11, year(datepart(&amp;amp;dttm))); */ /* DST ends 1st Sun in Nov     */
	if nwkdom(2, 1, 3, year(datepart(&amp;amp;dttm))) &amp;lt;= datepart(&amp;amp;dttm) &amp;lt;= nwkdom(1, 1,11, year(datepart(&amp;amp;dttm))) then do;
		&amp;amp;dttm = sum(&amp;amp;dttm, -"5:00:00"t);
	end;
	else do;
		&amp;amp;dttm = sum(&amp;amp;dttm, -"6:00:00"t);
	end;
%mend make_cst_from_gmt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Original macro to change dates:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1A;
	set table1;
    format DECN_DT  mmddyy10.;
	format DECN_TM  time8.;
    %make_cst_from_gmt1(dttime);
    DECN_DT = datepart(dttime);
    DECN_TM = timepart(dttime);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then I tried something like this by changing the original macro that does the conversion by adding an "if" statement. I also tried adding an "if" statement to my data step but I still get the date and time when the column has no data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro make_cst_from_gmt1(dttm);
	/* Change the date to CST from GMT */
	%if dttm &amp;gt; 0 %then %do;
		if nwkdom(2, 1, 3, year(datepart(&amp;amp;dttm))) &amp;lt;= datepart(&amp;amp;dttm) &amp;lt;= nwkdom(1, 1,11, year(datepart(&amp;amp;dttm))) then do;
			&amp;amp;dttm = sum(&amp;amp;dttm, -"5:00:00"t);
		end;
		else do;
			&amp;amp;dttm = sum(&amp;amp;dttm, -"6:00:00"t);
		end;
	%end;
%mend make_cst_from_gmt1;

data table1A;
	set table1;
    format DECN_DT  mmddyy10.;
	format DECN_TM  time8.;
    %make_cst_from_gmt1(dttime);
    DECN_DT = datepart(dttime);
    DECN_TM = timepart(dttime);
run;

data table1A;
	set table1;
    format DECN_DT  mmddyy10.;
	format DECN_TM  time8.;
	%if dttime = . %then %do;
		%put 'this one is missing ===&amp;gt; ' pgm_id;
		%put 'decn ===&amp;gt; ' dttime;
		DECN_DT = .
		DECN_TM = .
	%end;
	%else %do;
      %make_cst_from_gmt1(dttime);
      DECN_DT = datepart(dttime);
      DECN_TM = timepart(dttime);
	%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's some test data I created for this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
infile datalines;
input did pgm_id user $ dttime DATETIME24.3; 
format dttime DATETIME24.3;
return;
datalines;
8 63832680 DAN 23AUG2013:19:42:55.160
2 63832680 JAN 01AUG2013:19:20:58.785
6 63832680 DAN 
4 63832680 DAN 13AUG2013:19:29:39.578
7 63832680 DAN 23AUG2013:15:30:00.452
9 63832680 DAN 
1 63832680 BOB 14AUG2013:22:24:41.894
5 63832680 DAN 
3 63832680 DAN 08AUG2013:19:13:00.356
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Mar 2020 04:44:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-macro-to-convert-date-from-gmt-to-cst/m-p/633918#M188109</guid>
      <dc:creator>DanD999</dc:creator>
      <dc:date>2020-03-22T04:44:53Z</dc:date>
    </item>
    <item>
      <title>Re: help with macro to convert date from gmt to cst</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-macro-to-convert-date-from-gmt-to-cst/m-p/633920#M188111</link>
      <description>&lt;P&gt;The actual logic within the macro executes on SAS data step level and though you also need to build your logic on this level. Adding a simple check for a missing value for the SAS variable to which name macro variable &amp;amp;dttm resolves, will do the trick.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Required change added to below.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro make_cst_from_gmt(dttm);
  /* Change the date to CST from GMT */
  /* nwkdom(2, 1, 3, year(datepart(&amp;amp;dttm))); */
  /* DST begins 2nd Sun in March */
  /* nwkdom(1, 1,11, year(datepart(&amp;amp;dttm))); */
  /* DST ends 1st Sun in Nov     */
  if missing(&amp;amp;dttm) then; /* do nothing */
  else
  if nwkdom(2, 1, 3, year(datepart(&amp;amp;dttm))) &amp;lt;= datepart(&amp;amp;dttm) &amp;lt;= nwkdom(1, 1,11, year(datepart(&amp;amp;dttm))) then
    do;
      &amp;amp;dttm = sum(&amp;amp;dttm, -"5:00:00"t);
    end;
  else
    do;
      &amp;amp;dttm = sum(&amp;amp;dttm, -"6:00:00"t);
    end;
%mend make_cst_from_gmt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Given that the macro is already in use for years, eventually in a macro library and not that easily changed, another and eventually even better way is to check the variable for missing before calling the original unchanged macro. This also avoids that you then apply the datepart() function for missings - or you would have to implement such a missing check there anyway.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1A;
  set table1;
  format DECN_DT  mmddyy10.;
  format DECN_TM  time8.;
  if not missing(dttime) then
    do;
      %make_cst_from_gmt(dttime);
      DECN_DT = datepart(dttime);
      DECN_TM = timepart(dttime);
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Mar 2020 05:39:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-macro-to-convert-date-from-gmt-to-cst/m-p/633920#M188111</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-03-22T05:39:58Z</dc:date>
    </item>
    <item>
      <title>Re: help with macro to convert date from gmt to cst</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-macro-to-convert-date-from-gmt-to-cst/m-p/633921#M188112</link>
      <description>I had the right idea but poor execution. Thanks so much Patrick for the solution and quick response.</description>
      <pubDate>Sun, 22 Mar 2020 05:56:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-macro-to-convert-date-from-gmt-to-cst/m-p/633921#M188112</guid>
      <dc:creator>DanD999</dc:creator>
      <dc:date>2020-03-22T05:56:41Z</dc:date>
    </item>
  </channel>
</rss>

