<?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: %do loop to create variables for dates between two dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/do-loop-to-create-variables-for-dates-between-two-dates/m-p/900993#M356085</link>
    <description>&lt;P&gt;This line makes no sense&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to date_stop - date_start + 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The upper bound (the thing after the %TO) has to be a number.&amp;nbsp; So the macro processor called the %EVAL() function to convert the text you typed into a number.&amp;nbsp; But DATE_STOP and DATE_START are not numbers but long character strings.&amp;nbsp; So the %EVAL() could not find any number to calculate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you sure you didn't mean to write an actual DO statement?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i = 1 to date_stop - date_start + 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That would at least&amp;nbsp; make sense because to the data step the text string DATE_STOP is the name of a variable in the dataset being created.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To reference a variable using an index value, like your I variable, you need to use an ARRAY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you know you only want to make 5 new variables then just define the array that large.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array days date1 - date5;
  format date1-date5 yymmdd10.;
  do i=1 to max(5,date_stop - date_start + 1);
       days[i] = date_start + i -1;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: There no need to use INTNX() to increment date/time/datetime values by the units they are stored in.&amp;nbsp; Just increment by integer values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't have some upper bound on the number of variables you want to create then why make sure a difficult to use structure?&amp;nbsp; Why not just add only ONE new variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  do date=date_start to date_stop;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 01 Nov 2023 01:18:12 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-11-01T01:18:12Z</dc:date>
    <item>
      <title>%do loop to create variables for dates between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-to-create-variables-for-dates-between-two-dates/m-p/900983#M356079</link>
      <description>&lt;DIV&gt;I have two dates (start and stop), and I'm trying to create one column for each date between these two dates. Below is the summary of a macro I'm using. However, I'm getting this error: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. I was wondering if anyone could help me with this error. Thanks in advance!&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt;data have;
INFORMAT date_start date_stop MMDDYY10.;
input date_start date_stop ;
FORMAT date_start date_stop MMDDYY10.;
datalines;
1/15/2008 1/16/2008
1/28/2008 1/30/2008
2/12/2008 2/16/2008
;

data want;
INFORMAT date_start date_stop date_1 date_2 date_3 date_4 date_5 MMDDYY10.;
input date_start date_stop date_1 date_2 date_3 date_4 date_5;
FORMAT date_start date_stop date_1 date_2 date_3 date_4 date_5 MMDDYY10.;
datalines;
1/15/2008 1/16/2008 1/15/2008 1/16/2008
1/28/2008 1/30/2008 1/28/2008 1/29/2008 1/30/2008
2/12/2008 2/16/2008 2/12/2008 2/13/2008 2/14/2008 2/15/2008 2/16/2008
;

/* SAS macro code */

%macro test;
data want;
set have;
%do i = 1 %to date_stop - date_start + 1;
format date&amp;amp;i MMDDYY10.;
date&amp;amp;i = intnx('day', date_start, i);
%end;
run;
%mend;
%test&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 31 Oct 2023 22:53:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-to-create-variables-for-dates-between-two-dates/m-p/900983#M356079</guid>
      <dc:creator>aboloori</dc:creator>
      <dc:date>2023-10-31T22:53:23Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop to create variables for dates between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-to-create-variables-for-dates-between-two-dates/m-p/900986#M356081</link>
      <description>&lt;P&gt;I would not recommend a macro. Use a long data set (you may want to keep your data in this format, easier to work with typically) and then use transpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    INFORMAT date_start date_stop MMDDYY10.;
    input date_start date_stop;
    FORMAT date_start date_stop MMDDYY10.;
    datalines;
1/15/2008 1/16/2008
1/28/2008 1/30/2008
2/12/2008 2/16/2008
;

data long;
    set have;
    counter=0;

    do date=date_start to date_stop;
        counter+1;
        output;
    end;
run;

proc transpose data=long out=want prefix=date;
    by date_start date_stop;
    id counter;
    var date;
    format date: date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 31 Oct 2023 22:55:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-to-create-variables-for-dates-between-two-dates/m-p/900986#M356081</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-10-31T22:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop to create variables for dates between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-to-create-variables-for-dates-between-two-dates/m-p/900987#M356082</link>
      <description>And the macro language can't really access a variable value in the way you're using it in the %DO statement which is what generates the error. &lt;BR /&gt;</description>
      <pubDate>Tue, 31 Oct 2023 22:58:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-to-create-variables-for-dates-between-two-dates/m-p/900987#M356082</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-10-31T22:58:23Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop to create variables for dates between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-to-create-variables-for-dates-between-two-dates/m-p/900988#M356083</link>
      <description>&lt;P&gt;Not sure why you need a macro for this task.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below datastep code only. I'd go for a long table structure (want_long). If you choose a wide structure then you need to either pre-process your data to determine in advance how many array elements you need or you need to define a number that's certainly higher than what you ever would need.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want_long;
  set have;
  format date_want date9.;
  do date_want=date_start to date_stop;
    output;
  end;
run;

data want_wide(drop=_:);
  set have;
  array date_ {10};
  format date_: date9.;
  do _date=date_start to date_stop;
    date_[_date-date_start+1]=_date;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 31 Oct 2023 22:59:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-to-create-variables-for-dates-between-two-dates/m-p/900988#M356083</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-31T22:59:48Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop to create variables for dates between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-to-create-variables-for-dates-between-two-dates/m-p/900993#M356085</link>
      <description>&lt;P&gt;This line makes no sense&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to date_stop - date_start + 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The upper bound (the thing after the %TO) has to be a number.&amp;nbsp; So the macro processor called the %EVAL() function to convert the text you typed into a number.&amp;nbsp; But DATE_STOP and DATE_START are not numbers but long character strings.&amp;nbsp; So the %EVAL() could not find any number to calculate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you sure you didn't mean to write an actual DO statement?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i = 1 to date_stop - date_start + 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That would at least&amp;nbsp; make sense because to the data step the text string DATE_STOP is the name of a variable in the dataset being created.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To reference a variable using an index value, like your I variable, you need to use an ARRAY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you know you only want to make 5 new variables then just define the array that large.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array days date1 - date5;
  format date1-date5 yymmdd10.;
  do i=1 to max(5,date_stop - date_start + 1);
       days[i] = date_start + i -1;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: There no need to use INTNX() to increment date/time/datetime values by the units they are stored in.&amp;nbsp; Just increment by integer values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't have some upper bound on the number of variables you want to create then why make sure a difficult to use structure?&amp;nbsp; Why not just add only ONE new variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  do date=date_start to date_stop;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Nov 2023 01:18:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-to-create-variables-for-dates-between-two-dates/m-p/900993#M356085</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-11-01T01:18:12Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop to create variables for dates between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-to-create-variables-for-dates-between-two-dates/m-p/901022#M356094</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following (format statement borrowed from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;) identifies the maximum date range before making use of it in the final data step logic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* find the maximum date range that will be required */
data _null_;
  do until(last_obs);
    set have end = last_obs;
    max_range = max(max_range,date_stop-date_start+1);
  end;
  
  call symputx('max_range',max_range);
run;

%put &amp;amp;=max_range;

data want(drop = i);
  set have;

  array date_ [&amp;amp;max_range];
  format date_: MMDDYY10.;
  
  do i = date_start to date_stop;
    date_[i-date_start+1] = i;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Giving the output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Amir_0-1698826230068.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89296i92EDDC2A711C192D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Amir_0-1698826230068.png" alt="Amir_0-1698826230068.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 08:17:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-to-create-variables-for-dates-between-two-dates/m-p/901022#M356094</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2023-11-01T08:17:11Z</dc:date>
    </item>
  </channel>
</rss>

