<?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 Creating a variable from difference between two dates for use as end of a do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-from-difference-between-two-dates-for-use-as/m-p/246381#M46077</link>
    <description>&lt;P&gt;I have a data set that has a variable for start_date of a semester and end_date of the same semester. Because the number of days between these two dates will vary semester to semester, I want to dynamically count how many dates occur between (including start &amp;amp; end) these two dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I couldn't find a good data set to use in the sashelp library - so I'll work on creating a data set that I can upload. Uploading the code I'm using hoping that the mistake is obvious to those significantly more skilled than myself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data terms_temp_2;
	/*retain used here solely for the purpose of arranging variables*/
	/*for ease of reading*/
	retain intermediateDate sas_cmsd_start_date sas_cmsd_end_date sas_term_start_date sas_term_end_date totalTermDays rawDayOfSem;
	set terms_temp;

	/*convert SQL DDMMMYYYY:HH:MM:SS to sas equivalent of Date only*/
	sas_term_start_date = datepart(term_start_date);
	sas_term_end_date = datepart(term_end_date);
	sas_cmsd_start_date = datepart(cmsd_start_date);
	sas_cmsd_end_date = datepart(cmsd_end_date);

	/*calculate the difference between*/
	totalTermDays = sas_term_end_date - sas_term_start_date;


	/*create a variable from the above for use as "end" of a do loop*/
    /*in this instance, there are 115 days between start &amp;amp; end*/
    /*I want a dataset with an rawDayOfSem variable having values 1-115*/
	call symputx("totalTermDays",totalTermDays);
	%put &amp;amp;totalTermDays;

	/*create a new observation with the number of days*/
	/*since the start of the semester - First Day = 1*/
	
	do i = 1 to &amp;amp;totalTermDays;
		intermediateDate = sas_term_start_date + (i - 1);
		rawDayOfSem = i;
		output;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;However, I get an error that apparent sympolic reference to TotalTermDays not resolved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for any and all help.&lt;/P&gt;</description>
    <pubDate>Wed, 27 Jan 2016 16:21:55 GMT</pubDate>
    <dc:creator>GregG</dc:creator>
    <dc:date>2016-01-27T16:21:55Z</dc:date>
    <item>
      <title>Creating a variable from difference between two dates for use as end of a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-from-difference-between-two-dates-for-use-as/m-p/246381#M46077</link>
      <description>&lt;P&gt;I have a data set that has a variable for start_date of a semester and end_date of the same semester. Because the number of days between these two dates will vary semester to semester, I want to dynamically count how many dates occur between (including start &amp;amp; end) these two dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I couldn't find a good data set to use in the sashelp library - so I'll work on creating a data set that I can upload. Uploading the code I'm using hoping that the mistake is obvious to those significantly more skilled than myself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data terms_temp_2;
	/*retain used here solely for the purpose of arranging variables*/
	/*for ease of reading*/
	retain intermediateDate sas_cmsd_start_date sas_cmsd_end_date sas_term_start_date sas_term_end_date totalTermDays rawDayOfSem;
	set terms_temp;

	/*convert SQL DDMMMYYYY:HH:MM:SS to sas equivalent of Date only*/
	sas_term_start_date = datepart(term_start_date);
	sas_term_end_date = datepart(term_end_date);
	sas_cmsd_start_date = datepart(cmsd_start_date);
	sas_cmsd_end_date = datepart(cmsd_end_date);

	/*calculate the difference between*/
	totalTermDays = sas_term_end_date - sas_term_start_date;


	/*create a variable from the above for use as "end" of a do loop*/
    /*in this instance, there are 115 days between start &amp;amp; end*/
    /*I want a dataset with an rawDayOfSem variable having values 1-115*/
	call symputx("totalTermDays",totalTermDays);
	%put &amp;amp;totalTermDays;

	/*create a new observation with the number of days*/
	/*since the start of the semester - First Day = 1*/
	
	do i = 1 to &amp;amp;totalTermDays;
		intermediateDate = sas_term_start_date + (i - 1);
		rawDayOfSem = i;
		output;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;However, I get an error that apparent sympolic reference to TotalTermDays not resolved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for any and all help.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2016 16:21:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-from-difference-between-two-dates-for-use-as/m-p/246381#M46077</guid>
      <dc:creator>GregG</dc:creator>
      <dc:date>2016-01-27T16:21:55Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable from difference between two dates for use as end of a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-from-difference-between-two-dates-for-use-as/m-p/246385#M46080</link>
      <description>&lt;P&gt;Why not use the actual variable?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general you can't use a macro variable in the same step you create it, but you can use the actual variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;do i &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; to totalTermDays&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2016 16:25:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-from-difference-between-two-dates-for-use-as/m-p/246385#M46080</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-27T16:25:34Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable from difference between two dates for use as end of a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-from-difference-between-two-dates-for-use-as/m-p/246391#M46085</link>
      <description>&lt;P&gt;I have no idea. I thought I tried that and it didn't work, so I went the other route.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much! Just tested it and it works great!&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2016 16:33:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-from-difference-between-two-dates-for-use-as/m-p/246391#M46085</guid>
      <dc:creator>GregG</dc:creator>
      <dc:date>2016-01-27T16:33:52Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable from difference between two dates for use as end of a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-from-difference-between-two-dates-for-use-as/m-p/246392#M46086</link>
      <description>&lt;P&gt;Why are you trying to use the macro variable totalTermDays in a datastep Do loop when you have the perfectly valid variable totalTermDays available? Note that you are only going to have a value for the macro variable for the last record in your input set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And your phrase "how many dates occur between (including start &amp;amp; end) these two dates" may not quite match what you have done.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is some simplier code, does not rely on an unsupplied example data set, and tests the behavior of 2 known dates for start and end. Does this do what you expected?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
   sas_term_start_date = '01JAN2016'd;
   sas_term_end_date   = '10JAN2016'd;
   totalTermDays = sas_term_end_date - sas_term_start_date;
   do i = 1 to totalTermDays;
		intermediateDate = sas_term_start_date + (i - 1);
		rawDayOfSem = i;
		output;
	end;
   format sas_term_start_date sas_term_end_date intermediateDate mmddyy10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Jan 2016 16:33:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-from-difference-between-two-dates-for-use-as/m-p/246392#M46086</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-01-27T16:33:57Z</dc:date>
    </item>
  </channel>
</rss>

