<?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: Calculating variable name using do loops in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683795#M207145</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/110042"&gt;@sharonlee&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;, I tired the single dash but still get an error "ERROR: Array subscript out of range at line 1429 column 33."&lt;BR /&gt;I will transpose my variables to the long format.&lt;BR /&gt;Thanks.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You have to show us the &lt;EM&gt;entire&lt;/EM&gt; log for your DATA step. Otherwise, we can't possibly know what is line 1429 column 33.&lt;/P&gt;</description>
    <pubDate>Mon, 14 Sep 2020 22:33:41 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-09-14T22:33:41Z</dc:date>
    <item>
      <title>Calculating variable name using do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683753#M207122</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a dataset that looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;Visit_date_1&amp;nbsp;Visit_date_2 Visit_date_3 Visit_date_4 ....&amp;nbsp;Visit_date_n&lt;/P&gt;&lt;P&gt;I would like to calculate the number of months between&amp;nbsp;Visit_date_2 and&amp;nbsp;Visit_date_1, and&amp;nbsp;Visit_date_3 and&amp;nbsp;Visit_date_2, etc...&amp;nbsp; So essentially I am calculating&amp;nbsp;Visit_date_n -&amp;nbsp;Visit_date_(n-1).&lt;/P&gt;&lt;P&gt;I have written the following SAS code but it doesn't seem to work.&amp;nbsp; It says that "WARNING: Apparent symbolic reference I not resolved." and "ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &amp;amp;i.-1"&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Note, I am trying a new UNTIL loop which doesn't seem to like character values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any advice?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TIA&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data testing;&lt;BR /&gt;set testdate ;&lt;BR /&gt;do i = 2 to 100 until Visit_date_&amp;amp;i. = "" ;&lt;BR /&gt;%let j=%eval(&amp;amp;i.-1);&lt;BR /&gt;diff_month_i=intck('MONTH', Visit_date&amp;amp;i., Visit_date&amp;amp;j.);&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 19:09:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683753#M207122</guid>
      <dc:creator>sharonlee</dc:creator>
      <dc:date>2020-09-14T19:09:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating variable name using do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683758#M207123</link>
      <description>&lt;P&gt;You can't mix and match DATA step loops with MACRO loops. (Well, actually, you can, but this isn't the way to do it)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you need is a DATA step ARRAY with DATA step DO loop. Only DATA step structures are needed here. Something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testing;
    set testdate ;
    array v visit_date_1--visit_date_100; 
    array d delta_date_1--delta_date_100;
    do i = 2 to 100;
        d(i)=intck('month',v(i),v(i-1));
    end;
    drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Adding: the suggestion by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;is a preferable solution. Use a long data set, not a wide data set, and your programming will be much easier, not just on this problem, but on all problems where you have a choice between long and wide.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 19:47:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683758#M207123</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-14T19:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating variable name using do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683762#M207124</link>
      <description>&lt;P&gt;With a proper dataset structure (long vs. the unwieldy wide you have now), it boils down to this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
d = intck('month',lag(v),v);
if first.id then d = .;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 19:40:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683762#M207124</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-09-14T19:40:43Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating variable name using do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683773#M207130</link>
      <description>Thanks, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt; and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; for your responses. I will consider reformatting to a long dataset.&lt;BR /&gt;To answer my query, though, the delta_date_1 is not currently in my dataset. This is a new variable I want to create.&lt;BR /&gt;When I run the code provided by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;, I get this error "ERROR: Variable delta_date_1 cannot be found on the list of previously defined variables." When I remove the double hyphen (delta_date_1--delta_date_100 -&amp;gt; delta_date_1-delta_date_100) I get this error: "ERROR: Array subscript out of range at line 778 column 28." I looked up how to work with arrays, but I'm still confused why I am getting this error.&lt;BR /&gt;Can you please educate me on what I am doing wrong? Thank you.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 14 Sep 2020 20:10:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683773#M207130</guid>
      <dc:creator>sharonlee</dc:creator>
      <dc:date>2020-09-14T20:10:42Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating variable name using do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683775#M207131</link>
      <description>&lt;P&gt;I should have used a single dash. Beyond that, go for the long data set solution and avoid all these complications.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 20:19:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683775#M207131</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-14T20:19:00Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating variable name using do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683785#M207136</link>
      <description>Hi, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;, I tired the single dash but still get an error "ERROR: Array subscript out of range at line 1429 column 33."&lt;BR /&gt;I will transpose my variables to the long format.&lt;BR /&gt;Thanks.&lt;BR /&gt;</description>
      <pubDate>Mon, 14 Sep 2020 21:31:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683785#M207136</guid>
      <dc:creator>sharonlee</dc:creator>
      <dc:date>2020-09-14T21:31:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating variable name using do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683795#M207145</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/110042"&gt;@sharonlee&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;, I tired the single dash but still get an error "ERROR: Array subscript out of range at line 1429 column 33."&lt;BR /&gt;I will transpose my variables to the long format.&lt;BR /&gt;Thanks.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You have to show us the &lt;EM&gt;entire&lt;/EM&gt; log for your DATA step. Otherwise, we can't possibly know what is line 1429 column 33.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 22:33:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683795#M207145</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-14T22:33:41Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating variable name using do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683797#M207146</link>
      <description>&lt;P&gt;You are right,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;.&amp;nbsp; Thank you for pointing this out, and sorry for not including it earlier.&lt;/P&gt;&lt;P&gt;Here is a snap shot of my log (note I did not provide the variable list as it's quite lengthy.&amp;nbsp; Column 33 is actually one of the visit_date variables.).&lt;/P&gt;&lt;P&gt;The error is with my code:&amp;nbsp;&lt;STRONG&gt; d(i)=intck('month',v(i),v(i-1));&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1477&amp;nbsp; data testing;&lt;/P&gt;&lt;P&gt;1478&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set testdate;&lt;/P&gt;&lt;P&gt;1479&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; array v Visit_date__1--Visit_date__46;&lt;/P&gt;&lt;P&gt;1480&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; array d delta_date_1-delta_date_46;&lt;/P&gt;&lt;P&gt;1481&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do i = 1 to 46 ;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;1482&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; d(i)=intck('month',v(i),v(i-1));&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;1483&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;1484&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drop i;&lt;/P&gt;&lt;P&gt;1485&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: Array subscript out of range at line 1482 column 33.&lt;/P&gt;&lt;P&gt;&amp;lt;&amp;lt;list of variables&amp;gt;&amp;gt;&lt;/P&gt;&lt;P&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;/P&gt;&lt;P&gt;NOTE: There were 1 observations read from the data set testdate.&lt;/P&gt;&lt;P&gt;WARNING: The data set WORK.TESTING may be incomplete.&amp;nbsp; When this step was stopped there were 0 observations and 114&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; variables.&lt;/P&gt;&lt;P&gt;WARNING: Data set WORK.TESTING was not replaced because this step was stopped.&lt;/P&gt;&lt;P&gt;NOTE: DATA statement used (Total process time):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; real time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.04 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cpu time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.03 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 22:41:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683797#M207146</guid>
      <dc:creator>sharonlee</dc:creator>
      <dc:date>2020-09-14T22:41:18Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating variable name using do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683798#M207147</link>
      <description>&lt;P&gt;In your DO loop, when i=1, you are trying to refer to v(i-1) which is v(0) which doesn't exist. Or in SAS terms, "Array subscript out of range..." Elements in a SAS array are indexed by positive integers, and cannot be indexed by zero or negative integers or an integer greater than the number of elements in the array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Had the DO loop been 2 to 46, you would not have this error.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 22:48:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683798#M207147</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-14T22:48:29Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating variable name using do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683800#M207149</link>
      <description>&lt;P&gt;It works, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;!&amp;nbsp; Thank you for the explanation. I was trying a lot of different combinations but did not think of that!&lt;/P&gt;&lt;P&gt;Have a great day and thanks again.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 22:59:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-variable-name-using-do-loops/m-p/683800#M207149</guid>
      <dc:creator>sharonlee</dc:creator>
      <dc:date>2020-09-14T22:59:29Z</dc:date>
    </item>
  </channel>
</rss>

