<?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: Creating a date macro variable in a data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-date-macro-variable-in-a-data-step/m-p/857120#M338641</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There's a mistake in the code you posted, which makes it return an error.&amp;nbsp; I suspect in your real code, this line:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MyDate = &amp;amp;YearStartDate2; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is actually&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MyDate = &amp;amp;YearStart2; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that YearStart2 has the value:&amp;nbsp;2022-01-01.&lt;/P&gt;
&lt;P&gt;In the DATA step when you do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MyDate = &amp;amp;YearStart2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It will resolve to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MyDate = 2022-01-01;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which the data step compiler will see as subtraction so you get MyDate=2020.&lt;/P&gt;
&lt;P&gt;You could fix this by adding quotes around the value to make it character, but then you would also need to convert today() to character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you describe the big picture a bit?&amp;nbsp; If you have a macro variable with the value of a SAS date and you want a new macro variable with the date value formatted as MMDDYYD10, you can just use a PUTN statement to convert it, i.e.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let YearStart = %sysfunc(mdy(1,1,2022));
%let MyDate=%str(%')%sysfunc(putn(&amp;amp;YearStart,MMDDYYD10))%str(%') ;
%put &amp;amp;=MyDate ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Returns:&lt;/P&gt;
&lt;PRE&gt;91   %let YearStart = %sysfunc(mdy(1,1,2022));
92   %let MyDate=%str(%')%sysfunc(putn(&amp;amp;YearStart,MMDDYYD10))%str(%') ;
93   %put &amp;amp;=MyDate ;
MYDATE='01-01-2022'&lt;/PRE&gt;
&lt;P&gt;You can also use an open %IF statement to make it conditional, e.g. :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let refdate=2 ;

%if &amp;amp;refdate=1 %then %do ;
  %let MyDate=%str(%')%sysfunc(putn(%sysfunc(today()),MMDDYYD10))%str(%') ;
%end ;
%else %do ;
  %let MyDate=%str(%')%sysfunc(putn(&amp;amp;YearStart,MMDDYYD10))%str(%') ;
%end ;

%put &amp;amp;=MyDate ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 03 Feb 2023 21:47:05 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2023-02-03T21:47:05Z</dc:date>
    <item>
      <title>Creating a date macro variable in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-date-macro-variable-in-a-data-step/m-p/857118#M338639</link>
      <description>&lt;P&gt;I'm trying to create a macro variable containing a date inside of a data step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the reprex below, I'm trying to create a macro, MyDate, equal to the YearStart2 macro (1-1-2022).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I run the code below, the MyDate macro returns a value of '&amp;nbsp; &amp;nbsp; &amp;nbsp;2020', but I want it to return a value of '1-1-2022'. What am I doing wrong? Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/*Create dummy date*/
%let YearStart = %sysfunc(mdy(1,1,2022));
%put &amp;amp;YearStart;

%let YearStart2 = %sysfunc(putn(&amp;amp;YearStart,YYMMDDD10.));
%put &amp;amp;YearStart2;

%let refmonth = 2;

/*Set Date -- should be set to 1-1-2022*/
data _null_;
if &amp;amp;refmonth = 1 then do;
	MyDate = today();								
end;

else do;
	MyDate = &amp;amp;YearStartDate2; 								
end;

call symputx('MyDate',quote(MyDate, "'"));
run;

/*&amp;amp;MyDate is returning '     2020', but should be '1-1-2022'*/
%put &amp;amp;MyDate; %put &amp;amp;YearStart2&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Feb 2023 21:19:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-date-macro-variable-in-a-data-step/m-p/857118#M338639</guid>
      <dc:creator>everyone</dc:creator>
      <dc:date>2023-02-03T21:19:46Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a date macro variable in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-date-macro-variable-in-a-data-step/m-p/857119#M338640</link>
      <description>&lt;UL&gt;
&lt;LI&gt;variable name mismatch - (yearstart2 versus yearstart&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;date&lt;/STRONG&gt;&lt;/FONT&gt;2)&lt;/LI&gt;
&lt;LI&gt;Mixing of types, macro variables resolve as text, so you need to use INPUT to read the macro variable ((yearstart2) in as a SAS date&lt;/LI&gt;
&lt;LI&gt;Mixing of types - when applying the quote function, you need to first convert the date to the desired format to have it appear&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Create dummy date*/
%let YearStart = %sysfunc(mdy(1,1,2022));
%put &amp;amp;YearStart;

%let YearStart2 = %sysfunc(putn(&amp;amp;YearStart,YYMMDDD10.));
%put &amp;amp;YearStart2;

%let refmonth = 2;

/*Set Date -- should be set to 1-1-2022*/
data _null_;
if &amp;amp;refmonth = 1 then do;
	MyDate = today();								
end;

else do;
	MyDate = input("&amp;amp;YearStart2", yymmdd10.); 								
end;

call symputx('MyDate',quote(put(MyDate, mmddyyd10.), "'"));
run;

/*&amp;amp;MyDate is returning '     2020', but should be '1-1-2022'*/
%put &amp;amp;MyDate; %put &amp;amp;YearStart2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your code can also be simplified a bit (keeping the same idea:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let YearStart = %sysfunc(mdy(1,1,2022));
%put &amp;amp;YearStart;

%let refmonth = 2;

/*Set Date -- should be set to 1-1-2022*/
data _null_;
if &amp;amp;refmonth = 1 then do;
	MyDate = today();								
end;

else do;
	MyDate = &amp;amp;yearstart; 								
end;

call symputx('MyDate',quote(put(MyDate, mmddyyd10.), "'"));
run;

/*&amp;amp;MyDate is returning '     2020', but should be '1-1-2022'*/
%put &amp;amp;MyDate; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really want to drop the leading zeros I think you need to create a custom format.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll leave that up to you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/436074"&gt;@everyone&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I'm trying to create a macro variable containing a date inside of a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the reprex below, I'm trying to create a macro, MyDate, equal to the YearStart2 macro (1-1-2022).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I run the code below, the MyDate macro returns a value of '&amp;nbsp; &amp;nbsp; &amp;nbsp;2020', but I want it to return a value of '1-1-2022'. What am I doing wrong? Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/*Create dummy date*/
%let YearStart = %sysfunc(mdy(1,1,2022));
%put &amp;amp;YearStart;

%let YearStart2 = %sysfunc(putn(&amp;amp;YearStart,YYMMDDD10.));
%put &amp;amp;YearStart2;

%let refmonth = 2;

/*Set Date -- should be set to 1-1-2022*/
data _null_;
if &amp;amp;refmonth = 1 then do;
	MyDate = today();								
end;

else do;
	MyDate = &amp;amp;YearStartDate2; 								
end;

call symputx('MyDate',quote(MyDate, "'"));
run;

/*&amp;amp;MyDate is returning '     2020', but should be '1-1-2022'*/
%put &amp;amp;MyDate; %put &amp;amp;YearStart2&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;/LI-SPOILER&gt;</description>
      <pubDate>Fri, 03 Feb 2023 21:34:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-date-macro-variable-in-a-data-step/m-p/857119#M338640</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-02-03T21:34:58Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a date macro variable in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-date-macro-variable-in-a-data-step/m-p/857120#M338641</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There's a mistake in the code you posted, which makes it return an error.&amp;nbsp; I suspect in your real code, this line:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MyDate = &amp;amp;YearStartDate2; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is actually&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MyDate = &amp;amp;YearStart2; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that YearStart2 has the value:&amp;nbsp;2022-01-01.&lt;/P&gt;
&lt;P&gt;In the DATA step when you do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MyDate = &amp;amp;YearStart2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It will resolve to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MyDate = 2022-01-01;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which the data step compiler will see as subtraction so you get MyDate=2020.&lt;/P&gt;
&lt;P&gt;You could fix this by adding quotes around the value to make it character, but then you would also need to convert today() to character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you describe the big picture a bit?&amp;nbsp; If you have a macro variable with the value of a SAS date and you want a new macro variable with the date value formatted as MMDDYYD10, you can just use a PUTN statement to convert it, i.e.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let YearStart = %sysfunc(mdy(1,1,2022));
%let MyDate=%str(%')%sysfunc(putn(&amp;amp;YearStart,MMDDYYD10))%str(%') ;
%put &amp;amp;=MyDate ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Returns:&lt;/P&gt;
&lt;PRE&gt;91   %let YearStart = %sysfunc(mdy(1,1,2022));
92   %let MyDate=%str(%')%sysfunc(putn(&amp;amp;YearStart,MMDDYYD10))%str(%') ;
93   %put &amp;amp;=MyDate ;
MYDATE='01-01-2022'&lt;/PRE&gt;
&lt;P&gt;You can also use an open %IF statement to make it conditional, e.g. :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let refdate=2 ;

%if &amp;amp;refdate=1 %then %do ;
  %let MyDate=%str(%')%sysfunc(putn(%sysfunc(today()),MMDDYYD10))%str(%') ;
%end ;
%else %do ;
  %let MyDate=%str(%')%sysfunc(putn(&amp;amp;YearStart,MMDDYYD10))%str(%') ;
%end ;

%put &amp;amp;=MyDate ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Feb 2023 21:47:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-date-macro-variable-in-a-data-step/m-p/857120#M338641</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-02-03T21:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a date macro variable in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-date-macro-variable-in-a-data-step/m-p/857121#M338642</link>
      <description>&lt;P&gt;So you set YEARSTART to the first of 2022, which is the number 22,646.&lt;/P&gt;
&lt;P&gt;You then generate a version of if using the YYMMDD10. format, which will be 2022-01-01.&lt;/P&gt;
&lt;P&gt;You then use that same 2022-01-01 string in SAS code to set the variable MYDATE to the value 2,020.&amp;nbsp; Which is the date 1965-07-13.&amp;nbsp; You then convert the number 2,020 into a 12 character string and add quotes around so that you can store '&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2020' into the macro variable MYDATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to convert the string 2022-01-01 to an actual date value you will need to do something to it.&amp;nbsp; You could use INPUT() function with the right informat.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MyDate = input("&amp;amp;YearStart2",yymmdd10.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you could just use the actual date value you had in YEARSTART instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MyDate=&amp;amp;yearstart;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want the string to look pretty do the same think you did to make YEARSTART2, apply a FORMAT.&amp;nbsp; If you want it to look like YEARSTART2 then use the same format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call symputx('MyDate',quote(put(MyDate,yymmdd10.), "'"));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note if you use either the MMDDYY or DDMMYY format the result will look more like what you asked for '01-01-2022', but half of your audience will think the first two digits mean the month of the year and the other half will think they mean the day of the month.&amp;nbsp; Would '01-05-2022' mean the first of May or January the fifth?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Feb 2023 21:53:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-date-macro-variable-in-a-data-step/m-p/857121#M338642</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-03T21:53:39Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a date macro variable in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-date-macro-variable-in-a-data-step/m-p/857124#M338643</link>
      <description>&lt;P&gt;Hello&lt;BR /&gt;The corrected code is given below&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let YearStart = %sysfunc(mdy(1,1,2022));
%put &amp;amp;YearStart;

%let YearStart2 = %sysfunc(putn(&amp;amp;YearStart,DDMMYYD10.));
%put &amp;amp;YearStart2;

%let refmonth = 2;
/*Set Date -- should be set to 1-1-2022*/
data _null_;
Format Mydate DDMMYYD10.;
if &amp;amp;refmonth = 1 then do;
	MyDate = today();								
end;

else do;
	MyDate = &amp;amp;YearStart.; 	
	put Mydate=;
end;

call symputx('MyDate',put(Mydate,DDMMYYD10.));
run;

/*&amp;amp;MyDate is returning '     2020', but should be '1-1-2022'*/
%put &amp;amp;=MyDate; %put &amp;amp;=YearStart2.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The last put shows this&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sajid01_1-1675461468822.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80093i219EC73E944C6CBC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Sajid01_1-1675461468822.png" alt="Sajid01_1-1675461468822.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Feb 2023 21:58:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-date-macro-variable-in-a-data-step/m-p/857124#M338643</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2023-02-03T21:58:14Z</dc:date>
    </item>
  </channel>
</rss>

