<?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 How to set condition based on observation t-1 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667881#M200093</link>
    <description>&lt;P&gt;Hi everybody, I am a novice in SAS, and this is the first time I post my question on our forum, so I am using SAS EG at this moment. My data is simply transposed already as below:&lt;BR /&gt;The first column is the label of the companies (character type) (I have around 10,000 companies sorted ascendingly), the second column is about the date (ddmmyyyy9.) (daily data from 1/11987 to 31/12/2019),&lt;BR /&gt;and the third column is a numeric variable named "return index" (R)accordingly.&lt;BR /&gt;My purpose is to set the variable"R" as missing if (1 + Ri,d) * (1+ Ri,d-1) less than 50% and at least either Ri,d or Ri,d-1 is greater than 100% while Ri,d is the return of stock i (column Type) on day d (column date)&lt;BR /&gt;I am wondering if you can help me to solve this problem or give a hint to deal with it by using SAS (version 9.4) or SAS EG(version 8.2- 64 bit) (my preference).&lt;/P&gt;&lt;P&gt;Type&amp;nbsp; &amp;nbsp; &amp;nbsp;Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;R&lt;/P&gt;&lt;P&gt;131712 01JAN1987&amp;nbsp; &amp;nbsp; .&lt;BR /&gt;131712 02JAN1987&amp;nbsp; &amp;nbsp; .&lt;BR /&gt;131712 05JAN1987&amp;nbsp; &amp;nbsp; .&lt;BR /&gt;131712 06JAN1987&amp;nbsp; &amp;nbsp; .&lt;BR /&gt;131712 07JAN1987&amp;nbsp; &amp;nbsp; .&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;131712 31DEC2019&amp;nbsp; &amp;nbsp;67&lt;BR /&gt;28829X 01JAN1987&amp;nbsp; &amp;nbsp;89.5&lt;BR /&gt;28829X 02JAN1987&amp;nbsp; &amp;nbsp;89.7&lt;BR /&gt;28829X 02JAN1987&amp;nbsp; &amp;nbsp;90.66&lt;BR /&gt;28829X 02JAN1987&amp;nbsp; &amp;nbsp;91.34&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;28829X 31DEC2019&amp;nbsp; &amp;nbsp;85&lt;BR /&gt;.&lt;BR /&gt;Many thanks and cheers.&lt;/P&gt;</description>
    <pubDate>Wed, 08 Jul 2020 21:25:30 GMT</pubDate>
    <dc:creator>Phil_NZ</dc:creator>
    <dc:date>2020-07-08T21:25:30Z</dc:date>
    <item>
      <title>How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667881#M200093</link>
      <description>&lt;P&gt;Hi everybody, I am a novice in SAS, and this is the first time I post my question on our forum, so I am using SAS EG at this moment. My data is simply transposed already as below:&lt;BR /&gt;The first column is the label of the companies (character type) (I have around 10,000 companies sorted ascendingly), the second column is about the date (ddmmyyyy9.) (daily data from 1/11987 to 31/12/2019),&lt;BR /&gt;and the third column is a numeric variable named "return index" (R)accordingly.&lt;BR /&gt;My purpose is to set the variable"R" as missing if (1 + Ri,d) * (1+ Ri,d-1) less than 50% and at least either Ri,d or Ri,d-1 is greater than 100% while Ri,d is the return of stock i (column Type) on day d (column date)&lt;BR /&gt;I am wondering if you can help me to solve this problem or give a hint to deal with it by using SAS (version 9.4) or SAS EG(version 8.2- 64 bit) (my preference).&lt;/P&gt;&lt;P&gt;Type&amp;nbsp; &amp;nbsp; &amp;nbsp;Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;R&lt;/P&gt;&lt;P&gt;131712 01JAN1987&amp;nbsp; &amp;nbsp; .&lt;BR /&gt;131712 02JAN1987&amp;nbsp; &amp;nbsp; .&lt;BR /&gt;131712 05JAN1987&amp;nbsp; &amp;nbsp; .&lt;BR /&gt;131712 06JAN1987&amp;nbsp; &amp;nbsp; .&lt;BR /&gt;131712 07JAN1987&amp;nbsp; &amp;nbsp; .&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;131712 31DEC2019&amp;nbsp; &amp;nbsp;67&lt;BR /&gt;28829X 01JAN1987&amp;nbsp; &amp;nbsp;89.5&lt;BR /&gt;28829X 02JAN1987&amp;nbsp; &amp;nbsp;89.7&lt;BR /&gt;28829X 02JAN1987&amp;nbsp; &amp;nbsp;90.66&lt;BR /&gt;28829X 02JAN1987&amp;nbsp; &amp;nbsp;91.34&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;28829X 31DEC2019&amp;nbsp; &amp;nbsp;85&lt;BR /&gt;.&lt;BR /&gt;Many thanks and cheers.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 21:25:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667881#M200093</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2020-07-08T21:25:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667919#M200108</link>
      <description>&lt;DIV class="eJOY__extension_root_class"&gt;&lt;P&gt;Sorry, it is my faults about the format and data:&lt;BR /&gt;- daily data from 1/1/1987 instead of 1/111987&lt;BR /&gt;- And the data is fixed as below (I changed the dates of companies 28829X)&lt;/P&gt;&lt;P&gt;Type Date R&lt;/P&gt;&lt;P&gt;131712 01JAN1987 .&lt;BR /&gt;131712 02JAN1987 .&lt;BR /&gt;131712 05JAN1987 .&lt;BR /&gt;131712 06JAN1987 .&lt;BR /&gt;131712 07JAN1987 .&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;131712 31DEC2019 67&lt;BR /&gt;28829X 01JAN1987 89.5&lt;BR /&gt;28829X 02JAN1987 89.7&lt;BR /&gt;28829X 05JAN1987 90.66&lt;BR /&gt;28829X 06JAN1987 91.34&lt;BR /&gt;.&lt;BR /&gt;.&lt;/P&gt;&lt;P&gt;From my point of view, I thought that I may set up a new column R_1 that equal to Ri.d-1* Ri,d, then I will set missing if R_1 is less than 50% and (Ri,d or Ri,d-1 less than 100%).&lt;BR /&gt;Do you think this solution makes sense, and if it is the case, I am wondering if you can suggest me the codes to do so.&lt;/P&gt;&lt;/DIV&gt;&lt;DIV class="eJOY__extension_root_class"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Wed, 08 Jul 2020 23:41:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667919#M200108</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2020-07-08T23:41:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667920#M200109</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by type date;
run;

data want;
set have;
by type;
array _date(32*366) _temporary_;
array _r(32*366) _temporary_;
retain _date: _r: start;
if first.type then do;
 call missing(of _date:);
 call missing(of _r:);
 start = 0;
end;
start = start+1;
_date[start]=date;
_r[start] = r;
if last.date then do;
 do i = 1 to start;
    if i &amp;gt; 1 then do;
        if _r[i]*_r[i-1] &amp;lt; 50 and (r[i] &amp;lt; 100 or r[i-1] &amp;lt;100) then do;
              r=.;
              date = date[i];
              output;
         end;
         else do;
             r=r[i];
              date = date[i];
              output;
         end;
    end;
     else do;
          r=r[i];
              date = date[i];
              output;
     end;
 end; 
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Jul 2020 00:00:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667920#M200109</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-07-09T00:00:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667925#M200112</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;&amp;nbsp;, thank you very much for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately, when I put the codes into SAS, it results in some errors like that in my log, can you please have a look at it? Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;95         data want;
96         set have;
97         by type;
98         array _date(32*366) _temporary_;
                         _
                         22
                         200
99         array _r(32*366) _temporary_;
                      _
                      22
                      200
ERROR 22-322: Syntax error, expecting one of the following: ), ',', :.  

ERROR 200-322: The symbol is not recognized and will be ignored.

100        retain _date: _r: start;
101        if first.type then do;
102         call missing(of _date:);
                 _______
                 252
103         call missing(of _r:);
                 _______
                 252
ERROR 252-185: The MISSING subroutine call does not have enough arguments.

104         start = 0;
105        end;
106        start = start+1;
107        _date[start]=date;
108        _r[start] = r;
109        if last.date then do;
110         do i = 1 to start;
111            if i &amp;gt; 1 then do;
112                if _r[i]*_r[i-1] &amp;lt; 50 and (r[i] &amp;lt; 100 or r[i-1] &amp;lt;100) then do;
ERROR: Undeclared array referenced: r.
ERROR: Variable r has not been declared as an array.
ERROR: Undeclared array referenced: r.
ERROR: Variable r has not been declared as an array.
113                      r=.;
114                      date = date[i];
ERROR: Undeclared array referenced: date.
ERROR: Variable date has not been declared as an array.
115                      output;
116                 end;
117                 else do;
118                     r=r[i];
ERROR: Undeclared array referenced: r.
ERROR: Variable r has not been declared as an array.
119                      date = date[i];
ERROR: Undeclared array referenced: date.
ERROR: Variable date has not been declared as an array.
120                      output;
121                 end;
122            end;
123             else do;
124                  r=r[i];
ERROR: Undeclared array referenced: r.
ERROR: Variable r has not been declared as an array.
125                      date = date[i];
ERROR: Undeclared array referenced: date.
ERROR: Variable date has not been declared as an array.
126                      output;
127             end;
128         end;
129        end;
130        run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.WANT may be incomplete.  When this step was stopped there were 0 observations and 14 variables.
WARNING: Data set WORK.WANT was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Jul 2020 00:52:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667925#M200112</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2020-07-09T00:52:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667947#M200121</link>
      <description>Instead of 32*36 use 11712. Sorry my bad.</description>
      <pubDate>Thu, 09 Jul 2020 03:56:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667947#M200121</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-07-09T03:56:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667949#M200123</link>
      <description>You can try using lag function which can be simpler.&lt;BR /&gt;Data want;&lt;BR /&gt;Set have;&lt;BR /&gt;By type;&lt;BR /&gt;Lag_r =lag(R);&lt;BR /&gt;If lag_r*r &amp;lt; 50 and (lag_r&amp;lt;100 or r&amp;lt;100) then r1=.;&lt;BR /&gt;Else r1=r;&lt;BR /&gt;Run;</description>
      <pubDate>Thu, 09 Jul 2020 04:02:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667949#M200123</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-07-09T04:02:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667966#M200134</link>
      <description>&lt;P&gt;If you want to use a calculated value (to make the calculation obvious for the next developer), use a macro function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array _date{%eval(32*366)} _temporary_;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Jul 2020 06:36:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667966#M200134</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-09T06:36:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667995#M200150</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;&lt;/P&gt;&lt;P&gt;Thank you for your simpler version, but I think with this version, we can not separate the return(R) of different companies, am I correct? Because there would be one case that we multiple r of company 1 in 31/12/2019 with r of company 2 in 1/1/1987. Do you have any solution for it, I am really into this simple solution but it seems that it has not yet been completed.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2020 09:45:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667995#M200150</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2020-07-09T09:45:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667996#M200151</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;&lt;/P&gt;&lt;P&gt;Thank you for your correction, but when I change the number to 11712, there is another error emerged as below, can you please help me to have a look at it. Many thanks and cheers.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by type;
array _date(11712) _temporary_;
array _r(11712) _temporary_;
retain _date: _r: start;
if first.type then do;
 call missing(of _date:);
 call missing(of _r:);
 start = 0;
end;
start = start+1;
_date[start]=date;
_r[start] = r;
if last.date then do;
 do i = 1 to start;
    if i &amp;gt; 1 then do;
        if _r[i]*_r[i-1] &amp;lt;= 50 and (r[i] &amp;gt; 100 or r[i-1] &amp;gt; 100) then do;
              r=.;
              date = date[i];
              output;
         end;
         else do;
             r=r[i];
              date = date[i];
              output;
         end;
    end;
     else do;
          r=r[i];
              date = date[i];
              output;
     end;
 end; 
end;
run;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;28         data want;
29         set have;
30         by type;
31         array _date(11712) _temporary_;
32         array _r(11712) _temporary_;
33         retain _date: _r: start;
34         if first.type then do;
35          call missing(of _date:);
                 _______
                 252
36          call missing(of _r:);
                 _______
                 252
ERROR 252-185: The MISSING subroutine call does not have enough arguments.

37          start = 0;
38         end;
39         start = start+1;
40         _date[start]=date;
41         _r[start] = r;
42         if last.date then do;
43          do i = 1 to start;
44             if i &amp;gt; 1 then do;
45                 if _r[i]*_r[i-1] &amp;lt;= 50 and (r[i] &amp;gt; 100 or r[i-1] &amp;gt; 100) then do;
ERROR: Undeclared array referenced: r.
ERROR: Variable r has not been declared as an array.
ERROR: Undeclared array referenced: r.
ERROR: Variable r has not been declared as an array.
46                       r=.;
47                       date = date[i];
2                                                          The SAS System                               21:50 Thursday, July 9, 2020

ERROR: Undeclared array referenced: date.
ERROR: Variable date has not been declared as an array.
48                       output;
49                  end;
50                  else do;
51                      r=r[i];
ERROR: Undeclared array referenced: r.
ERROR: Variable r has not been declared as an array.
52                       date = date[i];
ERROR: Undeclared array referenced: date.
ERROR: Variable date has not been declared as an array.
53                       output;
54                  end;
55             end;
56              else do;
57                   r=r[i];
ERROR: Undeclared array referenced: r.
ERROR: Variable r has not been declared as an array.
58                       date = date[i];
ERROR: Undeclared array referenced: date.
ERROR: Variable date has not been declared as an array.
59                       output;
60              end;
61          end;
62         end;
63         
64         %LET _CLIENTTASKLABEL=;
65         %LET _CLIENTPROCESSFLOWNAME=;
66         %LET _CLIENTPROJECTPATH=;
67         %LET _CLIENTPROJECTPATHHOST=;
68         %LET _CLIENTPROJECTNAME=;
69         %LET _SASPROGRAMFILE=;
70         %LET _SASPROGRAMFILEHOST=;
71         
72         ;*';*";*/;quit;run;
                     ____
                     180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.WANT may be incomplete.  When this step was stopped there were 0 observations and 14 variables.
WARNING: Data set WORK.WANT was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds
      

73         ODS _ALL_ CLOSE;
74         
75         
76         QUIT; RUN;
77         &lt;/CODE&gt;&lt;/PRE&gt;&lt;DIV class="eJOY__extension_root_class"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Thu, 09 Jul 2020 10:00:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/667996#M200151</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2020-07-09T10:00:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668000#M200153</link>
      <description>&lt;P&gt;Testing revealed that you need to call the variables as a variable list:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.type then do;
 call missing(of _date1-_date11712);
 call missing(of _r1-_r11712);
 start = 0;
end;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Jul 2020 10:51:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668000#M200153</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-09T10:51:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668003#M200154</link>
      <description>&lt;P&gt;You also need to make sure that all array references are done with the correct names (leading underlines!).&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2020 11:11:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668003#M200154</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-09T11:11:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668010#M200157</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;&amp;nbsp;and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;CODE class=" language-sas"&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/CODE&gt;
&lt;PRE&gt;call missing(of _date1-_date11712);
call missing(of _r1-_r11712);&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This would create a lot of new numeric variables while leaving the values of the temporary array elements unchanged. To set the array elements to missing I would use:&lt;/P&gt;
&lt;PRE&gt;call missing(of _date&lt;FONT color="#000000"&gt;&lt;STRONG&gt;[*]&lt;/STRONG&gt;&lt;/FONT&gt;);
call missing(of _r&lt;FONT color="#000000"&gt;&lt;STRONG&gt;[*]&lt;/STRONG&gt;&lt;/FONT&gt;);&lt;/PRE&gt;
&lt;P&gt;Similarly, &lt;FONT face="courier new,courier"&gt;_date:&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;_r:&lt;/FONT&gt; in the RETAIN statement does not refer to the array elements, but to non-existing variables, while the temporary array elements are retained by default anyway.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2020 12:19:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668010#M200157</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-07-09T12:19:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668014#M200158</link>
      <description>&lt;P&gt;You are right!&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2020 12:28:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668014#M200158</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-09T12:28:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668020#M200159</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by type;
array _date(11712) _temporary_;
array _r(11712) _temporary_;
retain _date: _r: start;
if first.type then do;
 call missing(of _date1-_date11712);
 call missing(of _r1-_r11712);
 start = 0;
end;
start = start+1;
_date[start]=date;
_r[start] = r;
if last.date then do;
 do i = 1 to start;
    if i &amp;gt; 1 then do;
        if _r[i]*_r[i-1] &amp;lt;= 50 and (_r[i] &amp;gt; 100 or _r[i-1] &amp;gt; 100) then do;
              r=.;
              date = _date[i];
              output;
         end;
         else do;
             r=_r[i];
              date = _date[i];
              output;
         end;
    end;
     else do;
          r=_r[i];
              date = _date[i];
              output;
     end;
 end; 
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Jul 2020 12:47:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668020#M200159</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-07-09T12:47:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668149#M200228</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&amp;nbsp;@FreelanceReinhard&amp;nbsp;, and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thank you very much for your discussion and contribution, I benefit a lot from your conversation and codes as well.&lt;/P&gt;&lt;P&gt;After adding an update from&amp;nbsp;@FreelanceReinhard&amp;nbsp; to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;&amp;nbsp;'s solution (because I agreed that &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;'s former solution created too many new numeric variables), and change last.type to last.date (I thought it is incorrect if we put the last.date there because we did not set BY group for last date previously, so I deem it is your mistype, am I correct,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;&amp;nbsp;?, please let me know if my adjustment is wrong).&lt;BR /&gt;So, the final code seems to be as below, please let me know if you think it is incorrect or else&amp;nbsp;that I can close the topic. Once again, many thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by type;
array _date(11712) _temporary_;
array _r(11712) _temporary_;
retain _date: _r: start;
if first.type then do;
*I updated FreelanceReinhard's solution here;
  call missing(of _date[*]);
  call missing(of _r[*]);
 start = 0;
end;
start = start+1;
_date[start]=date;
_r[start] = r;

*I changed last.date to last.type here;

if last.type then do;
 do i = 1 to start;
    if i &amp;gt; 1 then do;
        if ((1 + _r[i]/100)*( 1 +_r[i-1]/100) -1) &amp;lt;= 0.5 and (_r[i] &amp;gt; 100 or _r[i-1] &amp;gt; 100) then do;
              r=.;
              date = _date[i];
              output;
         end;
         else do;
             r=_r[i];
              date = _date[i];
              output;
         end;
    end;
     else do;
          r=_r[i];
              date = _date[i];
              output;
     end;
 end; 
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Jul 2020 19:18:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668149#M200228</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2020-07-09T19:18:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668152#M200231</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Thank you for your simpler version, but I think with this version, we can not separate the return(R) of different companies, am I correct? Because there would be one case that we multiple r of company 1 in 31/12/2019 with r of company 2 in 1/1/1987. Do you have any solution for it, I am really into this simple solution but it seems that it has not yet been completed.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If the data is grouped then use BY group processing.&amp;nbsp; The code had a BY statement, but didn't use it for anything.&amp;nbsp; So reset the new LAG_R value to missing when you start a new group.&amp;nbsp; Since missing is less than any actual number that will mean the IF test will succeed and R1 will be missing for the first observation of each group.&amp;nbsp; &amp;nbsp;If you want something else for R1 for the first observation then change the logic to reflect that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by type;
  lag_r =lag(R);
  if first.type then lag_r=.;
  if lag_r*r &amp;lt; 50 and (lag_r&amp;lt;100 or r&amp;lt;100) then r1=.;
  else r1=r;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2020 19:27:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668152#M200231</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-09T19:27:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to set condition based on observation t-1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668156#M200235</link>
      <description>&lt;P&gt;Yes the code looks good to me. I learnt a lot too (the least important of which is never type code in an iphone)!&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2020 19:39:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-condition-based-on-observation-t-1/m-p/668156#M200235</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-07-09T19:39:04Z</dc:date>
    </item>
  </channel>
</rss>

