<?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 Comparing Observations by looping through 2 variables at a time in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Comparing-Observations-by-looping-through-2-variables-at-a-time/m-p/932775#M366931</link>
    <description>&lt;P&gt;I have a dataset that has the following vars - Account_Id, Position_1123 to Position_0224&lt;BR /&gt;(1123 refers to Nov'23, 1223 refers to Dec'23 and so on)&lt;BR /&gt;Each of the positions have values ., 1,2,3&lt;BR /&gt;I am trying to find movements in positions month-on-month beginning here with Dec'23 (Position_1223) to Feb'24 (Position_0224).&lt;BR /&gt;Each month is compared to previous month.&lt;/P&gt;&lt;P&gt;Logic :-&lt;BR /&gt;1. If current month = last month and both are not ., then 'Static'&lt;BR /&gt;2. If current month &amp;lt;&amp;gt; last month and both are either 1,2,3, then 'Moved'&lt;BR /&gt;3. If current month = . and last month was either 1,2,3, then 'Closed'&lt;BR /&gt;4. If current month is either 1,2,3 and last month was ., then 'New'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code:-&lt;/P&gt;&lt;DIV&gt;data test;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;input id $ Position_1123 Position_1223 Position_0124 Position_0224;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;datalines;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;A1 1 1 1 1&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;A2 1 1 2 2&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;A3 1 2 2 2&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;A4 . 1 1 2&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;A5 2 3 3 .&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;A6 2 2 3 3&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;A7 . . 1 2&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data want;&lt;/DIV&gt;&lt;DIV&gt;set test;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;format Position_mvmt $20.;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;array Positions {*} Position_1123 Position_1223 Position_0124 Position_0224;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;do i = 2 to dim(Positions);&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if Positions[i] = Positions[i-1] and Positions[i] in (1,2,3) then Position_mvmt = 'Static';&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;else if Positions[i] &amp;lt;&amp;gt; Positions[i-1] and Positions[i] in (1,2,3) and Positions[i-1] in (1,2,3) then Position_mvmt = 'Moved';&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;else if Positions[i] = . and Positions[i-1] in (1,2,3) then Position_mvmt = 'Closed';&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;else if Positions[i] in (1,2,3) and Positions[i-1] = . then Position_mvmt = 'New';&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;else Position_mvmt = 'Other';&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;end;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;drop i;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;P&gt;&lt;BR /&gt;Limitations:-&lt;BR /&gt;The code above compares and prints only the last 2 variables.&lt;BR /&gt;I want a method where it compares 1223 to 1123 and prints to Position_mvmt_1223 and then,&lt;BR /&gt;compare 0124 to 1223 to Position_mvmt_0124 and so on&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 18 Jun 2024 09:08:50 GMT</pubDate>
    <dc:creator>DrMD</dc:creator>
    <dc:date>2024-06-18T09:08:50Z</dc:date>
    <item>
      <title>Comparing Observations by looping through 2 variables at a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-Observations-by-looping-through-2-variables-at-a-time/m-p/932775#M366931</link>
      <description>&lt;P&gt;I have a dataset that has the following vars - Account_Id, Position_1123 to Position_0224&lt;BR /&gt;(1123 refers to Nov'23, 1223 refers to Dec'23 and so on)&lt;BR /&gt;Each of the positions have values ., 1,2,3&lt;BR /&gt;I am trying to find movements in positions month-on-month beginning here with Dec'23 (Position_1223) to Feb'24 (Position_0224).&lt;BR /&gt;Each month is compared to previous month.&lt;/P&gt;&lt;P&gt;Logic :-&lt;BR /&gt;1. If current month = last month and both are not ., then 'Static'&lt;BR /&gt;2. If current month &amp;lt;&amp;gt; last month and both are either 1,2,3, then 'Moved'&lt;BR /&gt;3. If current month = . and last month was either 1,2,3, then 'Closed'&lt;BR /&gt;4. If current month is either 1,2,3 and last month was ., then 'New'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code:-&lt;/P&gt;&lt;DIV&gt;data test;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;input id $ Position_1123 Position_1223 Position_0124 Position_0224;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;datalines;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;A1 1 1 1 1&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;A2 1 1 2 2&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;A3 1 2 2 2&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;A4 . 1 1 2&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;A5 2 3 3 .&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;A6 2 2 3 3&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;A7 . . 1 2&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data want;&lt;/DIV&gt;&lt;DIV&gt;set test;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;format Position_mvmt $20.;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;array Positions {*} Position_1123 Position_1223 Position_0124 Position_0224;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;do i = 2 to dim(Positions);&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if Positions[i] = Positions[i-1] and Positions[i] in (1,2,3) then Position_mvmt = 'Static';&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;else if Positions[i] &amp;lt;&amp;gt; Positions[i-1] and Positions[i] in (1,2,3) and Positions[i-1] in (1,2,3) then Position_mvmt = 'Moved';&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;else if Positions[i] = . and Positions[i-1] in (1,2,3) then Position_mvmt = 'Closed';&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;else if Positions[i] in (1,2,3) and Positions[i-1] = . then Position_mvmt = 'New';&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;else Position_mvmt = 'Other';&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;end;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;drop i;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;P&gt;&lt;BR /&gt;Limitations:-&lt;BR /&gt;The code above compares and prints only the last 2 variables.&lt;BR /&gt;I want a method where it compares 1223 to 1123 and prints to Position_mvmt_1223 and then,&lt;BR /&gt;compare 0124 to 1223 to Position_mvmt_0124 and so on&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2024 09:08:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-Observations-by-looping-through-2-variables-at-a-time/m-p/932775#M366931</guid>
      <dc:creator>DrMD</dc:creator>
      <dc:date>2024-06-18T09:08:50Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing Observations by looping through 2 variables at a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-Observations-by-looping-through-2-variables-at-a-time/m-p/932779#M366932</link>
      <description>Please provide a WANT dataset based on your TEST dataset input.</description>
      <pubDate>Tue, 18 Jun 2024 09:57:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-Observations-by-looping-through-2-variables-at-a-time/m-p/932779#M366932</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2024-06-18T09:57:50Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing Observations by looping through 2 variables at a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-Observations-by-looping-through-2-variables-at-a-time/m-p/932782#M366933</link>
      <description>&lt;P&gt;Hi, Please refer to the Excel that shows this.&amp;nbsp;&lt;BR /&gt;I might have got the loop right but not able to get the following variables :-&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Position_Mvmt_1223&lt;/TD&gt;&lt;TD&gt;Position_Mvmt_0124&lt;/TD&gt;&lt;TD&gt;Position_Mvmt_0224&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&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="DrMD_0-1718707027893.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97546i347777CE8096951E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="DrMD_0-1718707027893.png" alt="DrMD_0-1718707027893.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;What should I do to loop and print (this means replacing&amp;nbsp;Position_mvmt in the existing code) ?&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2024 10:38:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-Observations-by-looping-through-2-variables-at-a-time/m-p/932782#M366933</guid>
      <dc:creator>DrMD</dc:creator>
      <dc:date>2024-06-18T10:38:59Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing Observations by looping through 2 variables at a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-Observations-by-looping-through-2-variables-at-a-time/m-p/932787#M366934</link>
      <description>&lt;P&gt;This is close to what you asked for. You can assign formats to the MVMT variables to contain the exact text you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set test;
    array pos pos:;
    array mvmt(3) $;
    do i=1 to dim(pos)-1;
        mvmt(i)=cats(pos(i),pos(i+1));
    end;
    drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I add that the layout of your data, with calendar information in the variable name, is something that you should particularly avoid, as this makes programming difficult.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I transpose the data to a long format to show what a long format data set looks like, this is really the way your data should be arranged, with calendar information as the values of variable ym.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=test out=transp;
    by id;
run;
data long;
    set transp;
    m=input(substr(_name_,10,2),2.);
    y=input(substr(_name_,12,2),2.);
    ym=mdy(m,1,2000+y);
    format ym yymmn6.;
    drop m y _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have the data in the transposed (long) form, then no looping is needed. Starting with long, the code is simple.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set long;
    by id;
    lag_col1=lag(col1);
    if not first.id then mvmt=cats(lag_col1,col1);
    drop lag_col1 _name_;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now you have all your data and calculations in one data set that can be used easily. If necessary you can assign a format to the mvmt variable so it appears as the text you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You want a table output? No problem with PROC REPORT&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc report data=want;
    columns ("ID" id) ("Position" col1),ym ("Movement" mvmt),ym dummy;
    define id /' ' group;
    define col1/ ' ';
    define ym/' ' across;
    define mvmt/' ';
    define dummy/noprint;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One other point: your dates are mmyy, this again is a particularly poor way to represent calendar dates, SAS and also humans work much better with dates that are yymm.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2024 13:30:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-Observations-by-looping-through-2-variables-at-a-time/m-p/932787#M366934</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-06-18T13:30:22Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing Observations by looping through 2 variables at a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-Observations-by-looping-through-2-variables-at-a-time/m-p/932875#M366946</link>
      <description>&lt;P&gt;Thank you very much.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Great work and idea!&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2024 16:17:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-Observations-by-looping-through-2-variables-at-a-time/m-p/932875#M366946</guid>
      <dc:creator>DrMD</dc:creator>
      <dc:date>2024-06-18T16:17:07Z</dc:date>
    </item>
  </channel>
</rss>

