<?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: Compute variables in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/774941#M37913</link>
    <description>&lt;P&gt;Ok. Try this then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID v1 v2;
datalines;
1 1 1
1 7 2
2 16 1
2 3 2
3 11 1
4 13 1
4 3 2
5 4 1
5 3 2
5 2 3
5 2 4
6 2 1
6 4 2
6 3 3
;

data want(drop = c n);

   do _N_ = 1 by 1 until (last.ID);
      set have;
     by ID;

      if first.ID then c = 0;

      array vv1{999} _temporary_;
      array vv2{999} _temporary_;
     
      c + v1;

      vv1 [_N_] = c;
      vv2 [_N_] = v2;
   end;

   n = _N_;
   v4 = c;

   do _N_ = 1 to _N_;
      set have;

      v3 = vv2[1 + n - _N_];
      v4 = v4 - ifn(_N_ &amp;gt; 1, lag(v1), 0);
      v5 = (v4 - v1) * (v3 ne 1);

      output;
   end;

   call missing(of vv1[*], of vv2[*]);

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Obs	ID	v1	v2	v4	v3	v5
1	1	1	1	8	2	7
2	1	7	2	7	1	0
3	2	16	1	19	2	3
4	2	3	2	3	1	0
5	3	11	1	11	1	0
6	4	13	1	16	2	3
7	4	3	2	3	1	0
8	5	4	1	11	4	7
9	5	3	2	7	3	4
10	5	2	3	4	2	2
11	5	2	4	2	1	0
12	6	2	1	9	3	7
13	6	4	2	7	2	3
14	6	3	3	3	1	0&lt;/PRE&gt;</description>
    <pubDate>Mon, 18 Oct 2021 17:27:09 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2021-10-18T17:27:09Z</dc:date>
    <item>
      <title>Compute variables</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/774837#M37895</link>
      <description>&lt;P&gt;I have data consists ID, V1, and V2 for around 11000 observations. The sample format is given below for six observations.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAS.PNG" style="width: 202px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64811i9575860CE28B3991/image-size/large?v=v2&amp;amp;px=999" role="button" title="SAS.PNG" alt="SAS.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;From this data, I wanna create V3, V4, and V5 as follows:&lt;/P&gt;&lt;P&gt;V3 is the inverse order of V2. Example for ID=1, V2= (2,1) then V3=(1,2).&lt;/P&gt;&lt;P&gt;V4 Is the cumulative greater type of V1 (in order of V3). Example for ID=1, V1=(1, 7) then V4=(7,8).&lt;/P&gt;&lt;P&gt;V5 is always equal to 0 if V3=1, else V5 is equal to lag1 of V4.&lt;/P&gt;&lt;P&gt;The expected output that I did manually is given below:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAS2.PNG" style="width: 393px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64812iD8D9D82F9E8CFCD6/image-size/large?v=v2&amp;amp;px=999" role="button" title="SAS2.PNG" alt="SAS2.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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Oct 2021 07:35:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/774837#M37895</guid>
      <dc:creator>Lijuu</dc:creator>
      <dc:date>2021-10-18T07:35:55Z</dc:date>
    </item>
    <item>
      <title>Re: Compute variables</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/774871#M37901</link>
      <description>&lt;P&gt;This gives you v3 and v4.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regarding v5. You say you want the &lt;STRONG&gt;&lt;EM&gt;lag&lt;/EM&gt;&lt;/STRONG&gt; of v4. Though your desired results suggest you want the &lt;EM&gt;&lt;STRONG&gt;lead&lt;/STRONG&gt;&lt;/EM&gt; of v4?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID v1 v2;
datalines;
1 1 1
1 7 2
2 16 1
2 3 2
3 11 1
4 13 1
4 3 2
5 4 1
5 3 2
5 2 3
5 2 4
6 2 1
6 4 2
6 3 3
;

data want(drop = c n);

   do _N_ = 1 by 1 until (last.ID);
      set have;
	  by ID;

      if first.ID then c = 0;

      array vv1{999} _temporary_;
	  array vv2{999} _temporary_;
	  
	  c + v1;

	  vv1 [_N_] = c;
      vv2 [_N_] = v2;
   end;

   n = _N_;
   v4 = c;

   do _N_ = 1 to _N_;
      set have;

      v3 = vv2[1 + n - _N_];
	  v4 = v4 - ifn(_N_ &amp;gt; 1, lag(v1), 0);

	  output;
   end;

   call missing(of vv1[*], of vv2[*]);

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID  v1  v2  v4  v3 
1   1   1   8   2 
1   7   2   7   1 
2   16  1   19  2 
2   3   2   3   1 
3   11  1   11  1 
4   13  1   16  2 
4   3   2   3   1 
5   4   1   11  4 
5   3   2   7   3 
5   2   3   4   2 
5   2   4   2   1 
6   2   1   9   3 
6   4   2   7   2 
6   3   3   3   1 
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Oct 2021 12:45:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/774871#M37901</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-10-18T12:45:55Z</dc:date>
    </item>
    <item>
      <title>Re: Compute variables</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/774885#M37903</link>
      <description>Yes Indeed! V5 needs to be the lead of v4 as like as the suggested results. Thank you very much, Dear.</description>
      <pubDate>Mon, 18 Oct 2021 13:49:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/774885#M37903</guid>
      <dc:creator>Lijuu</dc:creator>
      <dc:date>2021-10-18T13:49:47Z</dc:date>
    </item>
    <item>
      <title>Re: Compute variables</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/774941#M37913</link>
      <description>&lt;P&gt;Ok. Try this then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID v1 v2;
datalines;
1 1 1
1 7 2
2 16 1
2 3 2
3 11 1
4 13 1
4 3 2
5 4 1
5 3 2
5 2 3
5 2 4
6 2 1
6 4 2
6 3 3
;

data want(drop = c n);

   do _N_ = 1 by 1 until (last.ID);
      set have;
     by ID;

      if first.ID then c = 0;

      array vv1{999} _temporary_;
      array vv2{999} _temporary_;
     
      c + v1;

      vv1 [_N_] = c;
      vv2 [_N_] = v2;
   end;

   n = _N_;
   v4 = c;

   do _N_ = 1 to _N_;
      set have;

      v3 = vv2[1 + n - _N_];
      v4 = v4 - ifn(_N_ &amp;gt; 1, lag(v1), 0);
      v5 = (v4 - v1) * (v3 ne 1);

      output;
   end;

   call missing(of vv1[*], of vv2[*]);

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Obs	ID	v1	v2	v4	v3	v5
1	1	1	1	8	2	7
2	1	7	2	7	1	0
3	2	16	1	19	2	3
4	2	3	2	3	1	0
5	3	11	1	11	1	0
6	4	13	1	16	2	3
7	4	3	2	3	1	0
8	5	4	1	11	4	7
9	5	3	2	7	3	4
10	5	2	3	4	2	2
11	5	2	4	2	1	0
12	6	2	1	9	3	7
13	6	4	2	7	2	3
14	6	3	3	3	1	0&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Oct 2021 17:27:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/774941#M37913</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-10-18T17:27:09Z</dc:date>
    </item>
    <item>
      <title>Re: Compute variables</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/775117#M37932</link>
      <description>&lt;PRE&gt;data have;
input ID v1 v2;
datalines;
1 1 1
1 7 2
2 16 1
2 3 2
3 11 1
4 13 1
4 3 2
5 4 1
5 3 2
5 2 3
5 2 4
6 2 1
6 4 2
6 3 3
;
data temp;
 set have;
 by id;
 if first.id then n=0;
 n+1;
run;
proc sort data=temp;by id descending n;run;
data temp1(keep=v3) temp2(keep=id n v4);
 set temp;
 by id;
 v3=v2;
 if first.id then v4=0;
 v4+v1;
 run;
 proc sort data=temp2;by id n;run;
proc sort data=temp2 out=temp3;by id  descending n;run;
data temp3;
 set temp3;
 by id;
 v5=lag(v4);
 if first.id then v5=.;
run;
proc sort data=temp3;by id n;run;

data want;
 merge have temp1 temp2(keep=v4) temp3(keep=v5); 
 if v3=1 then v5=0;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Oct 2021 13:27:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/775117#M37932</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-10-19T13:27:23Z</dc:date>
    </item>
    <item>
      <title>Re: Compute variables</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/775323#M37947</link>
      <description>Thank you very much, Dear.&lt;BR /&gt;It is successfully working.</description>
      <pubDate>Wed, 20 Oct 2021 07:20:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/775323#M37947</guid>
      <dc:creator>Lijuu</dc:creator>
      <dc:date>2021-10-20T07:20:23Z</dc:date>
    </item>
    <item>
      <title>Re: Compute variables</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/775324#M37948</link>
      <description>Thank you very much, Dear!</description>
      <pubDate>Wed, 20 Oct 2021 07:20:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/775324#M37948</guid>
      <dc:creator>Lijuu</dc:creator>
      <dc:date>2021-10-20T07:20:55Z</dc:date>
    </item>
    <item>
      <title>Re: Compute variables</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/775325#M37949</link>
      <description>&lt;P&gt;Anytime &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Oct 2021 07:27:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Compute-variables/m-p/775325#M37949</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-10-20T07:27:05Z</dc:date>
    </item>
  </channel>
</rss>

