<?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: sas code for increasing and decreasing numbers in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860939#M340083</link>
    <description>&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;but I get this&amp;nbsp;&lt;/P&gt;&lt;P&gt;"ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the&lt;BR /&gt;BY expression is missing, zero, or invalid."...&lt;/P&gt;&lt;P&gt;Any idea why?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 26 Feb 2023 15:28:01 GMT</pubDate>
    <dc:creator>HEB1</dc:creator>
    <dc:date>2023-02-26T15:28:01Z</dc:date>
    <item>
      <title>sas code for increasing and decreasing numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860924#M340080</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;my data looks like that:&lt;/P&gt;&lt;P&gt;ID year post&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 1990 0&lt;/P&gt;&lt;P&gt;1 1991 0&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 1992 0&lt;/P&gt;&lt;P&gt;1 1993 1&lt;/P&gt;&lt;P&gt;1 1994 1&lt;/P&gt;&lt;P&gt;2 2000 0&lt;/P&gt;&lt;P&gt;2 2001 0&amp;nbsp;&lt;/P&gt;&lt;P&gt;2 2002 1&lt;/P&gt;&lt;P&gt;2 2002 1&lt;/P&gt;&lt;P&gt;2 2003 1&lt;/P&gt;&lt;P&gt;I want to add a variable so my data will look like that-&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID year post&amp;nbsp; b_f&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 1990 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-3&lt;/P&gt;&lt;P&gt;1 1991 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-2&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 1992 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-1&lt;/P&gt;&lt;P&gt;1 1993 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;1 1994 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;2 2000 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -2&lt;/P&gt;&lt;P&gt;2 2001 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-1&lt;/P&gt;&lt;P&gt;2 2002 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;2 2002 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;2 2003 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks&lt;/P&gt;</description>
      <pubDate>Sun, 26 Feb 2023 12:54:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860924#M340080</guid>
      <dc:creator>HEB1</dc:creator>
      <dc:date>2023-02-26T12:54:45Z</dc:date>
    </item>
    <item>
      <title>Re: sas code for increasing and decreasing numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860925#M340081</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID year post;
datalines;
1 1990 0 
1 1991 0 
1 1992 0 
1 1993 1 
1 1994 1 
2 2000 0 
2 2001 0 
2 2002 1 
2 2002 1 
2 2003 1 
;

data want(keep = ID year post b_f);

   flag = 0;

   do _N_ = 0 by 1 until (last.ID);
      set have;
      by ID;
      if post = 1 and flag = 0 then do;
         nn = _N_; flag = 1;
      end;
   end;

   do b_f = -nn by 1 until (last.ID);
      set have;
      by ID;
      if b_f = 0 then b_f + 1;
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID  year  post  b_f
1   1990  0     -3
1   1991  0     -2
1   1992  0     -1
1   1993  1      1
1   1994  1      2
2   2000  0     -2
2   2001  0     -1
2   2002  1      1
2   2002  1      2
2   2003  1      3&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Feb 2023 13:10:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860925#M340081</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2023-02-26T13:10:07Z</dc:date>
    </item>
    <item>
      <title>Re: sas code for increasing and decreasing numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860939#M340083</link>
      <description>&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;but I get this&amp;nbsp;&lt;/P&gt;&lt;P&gt;"ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the&lt;BR /&gt;BY expression is missing, zero, or invalid."...&lt;/P&gt;&lt;P&gt;Any idea why?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Feb 2023 15:28:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860939#M340083</guid>
      <dc:creator>HEB1</dc:creator>
      <dc:date>2023-02-26T15:28:01Z</dc:date>
    </item>
    <item>
      <title>Re: sas code for increasing and decreasing numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860940#M340084</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/148688"&gt;@HEB1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;but I get this&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the&lt;BR /&gt;BY expression is missing, zero, or invalid."...&lt;/P&gt;
&lt;P&gt;Any idea why?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That probably indicates a by group where there are no 1's detected.&lt;/P&gt;
&lt;P&gt;You need to explain how you want those cases numbered.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also why did you skip zero in your numbering?&lt;/P&gt;</description>
      <pubDate>Sun, 26 Feb 2023 15:32:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860940#M340084</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-26T15:32:07Z</dc:date>
    </item>
    <item>
      <title>Re: sas code for increasing and decreasing numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860942#M340086</link>
      <description>&lt;P&gt;MY ID&amp;nbsp; variable is sting, with letters- is that a problem?&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not want to skip zero. I will write again.&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data is&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID YEAR POST&lt;/P&gt;&lt;P&gt;A 1990 0&lt;BR /&gt;A 1991 0&lt;BR /&gt;A 1992 0&lt;BR /&gt;A 1993 1&lt;BR /&gt;A 1994 1&lt;BR /&gt;B 2000 0&lt;BR /&gt;B 2001 0&lt;BR /&gt;B 2002 1&lt;BR /&gt;B 2002 1&lt;BR /&gt;B 2003 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;AND I WANT- zero on the first post of the ID:&lt;/P&gt;&lt;P&gt;ID YEAR POST B_F&lt;/P&gt;&lt;P&gt;A 1990 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-3&lt;BR /&gt;A 1991 0&amp;nbsp; &amp;nbsp; &amp;nbsp; -2&lt;BR /&gt;A 1992 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -1&lt;BR /&gt;A 1993 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;A 1994 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;B 2000 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -2&lt;BR /&gt;B 2001 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-1&lt;BR /&gt;B 2002 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;B 2002 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;B 2003 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Feb 2023 15:45:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860942#M340086</guid>
      <dc:creator>HEB1</dc:creator>
      <dc:date>2023-02-26T15:45:31Z</dc:date>
    </item>
    <item>
      <title>Re: sas code for increasing and decreasing numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860944#M340087</link>
      <description>&lt;P&gt;What do you want to do when some value of ID does not have any YEAR with POST=1 ?&amp;nbsp; How do you want to number those observations?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want them to number up to -1 then you could do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID $ YEAR POST ;
cards;
A 1990 0
A 1991 0
A 1992 0
A 1993 1
A 1994 1
B 2000 0
B 2001 0
B 2002 1
B 2002 1
B 2003 1
C 1990 0
C 1991 0
C 1992 0
;

data want;
  do _n_=1 by 1 until(last.id);
    set have;
    by id;
    if post=1 and missing(event_year) then event_year=year;
  end;
  if missing(event_year) then event_year=year+1;
  do _n_=1 to _n_;
    set have;
    b_f = year-event_year ;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;                             event_
Obs    ID    YEAR    POST     year     b_f

  1    A     1990      0      1993      -3
  2    A     1991      0      1993      -2
  3    A     1992      0      1993      -1
  4    A     1993      1      1993       0
  5    A     1994      1      1993       1
  6    B     2000      0      2002      -2
  7    B     2001      0      2002      -1
  8    B     2002      1      2002       0
  9    B     2002      1      2002       0
 10    B     2003      1      2002       1
 11    C     1990      0      1993      -3
 12    C     1991      0      1993      -2
 13    C     1992      0      1993      -1
&lt;/PRE&gt;
&lt;P&gt;I calculated an "event" year instead of just numbering because that would handle the case where some year is missing in the middle, or in your data assign the same B_F flag to the two records that are both for the year 2002.&amp;nbsp; If you don't want to do that then change the logic to just use the row counter variable instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do _n_=1 by 1 until(last.id);
    set have;
    by id;
    if post=1 and missing(event_row) then event_row=_n_;
  end;
  if missing(event_row) then event_row=_n_+1;
  do _n_=1 to _n_;
    set have;
    b_f = _n_-event_row ;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS Learn to use the Insert Code and Insert SAS Code icons on the forum editor to insert your test blocks and SAS code blocks. That will prevent the forum from trying to flow the text into paragraphs.&lt;/P&gt;</description>
      <pubDate>Sun, 26 Feb 2023 16:02:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860944#M340087</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-26T16:02:43Z</dc:date>
    </item>
    <item>
      <title>Re: sas code for increasing and decreasing numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860946#M340088</link>
      <description>&lt;P&gt;Thank you! it is great! I dont have any missing years so I used the last option. It is Awesome.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Feb 2023 16:10:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-code-for-increasing-and-decreasing-numbers/m-p/860946#M340088</guid>
      <dc:creator>HEB1</dc:creator>
      <dc:date>2023-02-26T16:10:58Z</dc:date>
    </item>
  </channel>
</rss>

