<?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 track changes over time in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/track-changes-over-time/m-p/928967#M365536</link>
    <description>&lt;P&gt;I have long data and wrote code to track changes in 2 variables over time. However, the second entry for each userID is wrong. My data, code, and screenshot of results below. What is wrong with the code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fake_data;
input userID $ event_date date9. response $ code $;
format event_date date9.;
datalines;
1693 01Dec2014 Y V
1693 01Jan2015 Y V
1693 01Feb2015 Y V
1693 01Mar2015 M G
1693 01Apr2015 M G
1693 01May2015 M G
1129 01Feb2018 Y V
1129 01Mar2018 Y V
1129 01Apr2018 N R
1129 01May2018 N R
1129 01Jun2018 M R
1129 01Jul2018 M R 
1345 01Aug2016 N R
1345 01Sep2016 N R
1345 01Oct2016 N R
1345 01Nov2016 N R
1345 01Dec2016 M R
1345 01Jan2017 M G
1345 01Feb2017 Y G
1345 01Mar2017 Y G
1345 01Apr2017 Y G
1345 01May2017 Y G
;

proc sort data = fake_data out=fake_data_nodupkey nodupkey; by userID event_date; run;
data flag_vars;
set fake_data_nodupkey;
by userID event_date;
if first.userID then do;
	prev_response = response;
	prev_code = code;
	end;
else do;
	prev_response = lag(response);
	prev_code = lag(code);
	end;

response_change = 0;
if response ne prev_response then response_change = 1;

code_change = 0;
if code ne prev_code then code_change = 1;

run;

proc print data=flag_vars;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="results.PNG" style="width: 986px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96627i7A10CF4D2CE39DB0/image-size/large?v=v2&amp;amp;px=999" role="button" title="results.PNG" alt="results.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 20 May 2024 01:51:18 GMT</pubDate>
    <dc:creator>axescot78</dc:creator>
    <dc:date>2024-05-20T01:51:18Z</dc:date>
    <item>
      <title>track changes over time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/track-changes-over-time/m-p/928967#M365536</link>
      <description>&lt;P&gt;I have long data and wrote code to track changes in 2 variables over time. However, the second entry for each userID is wrong. My data, code, and screenshot of results below. What is wrong with the code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fake_data;
input userID $ event_date date9. response $ code $;
format event_date date9.;
datalines;
1693 01Dec2014 Y V
1693 01Jan2015 Y V
1693 01Feb2015 Y V
1693 01Mar2015 M G
1693 01Apr2015 M G
1693 01May2015 M G
1129 01Feb2018 Y V
1129 01Mar2018 Y V
1129 01Apr2018 N R
1129 01May2018 N R
1129 01Jun2018 M R
1129 01Jul2018 M R 
1345 01Aug2016 N R
1345 01Sep2016 N R
1345 01Oct2016 N R
1345 01Nov2016 N R
1345 01Dec2016 M R
1345 01Jan2017 M G
1345 01Feb2017 Y G
1345 01Mar2017 Y G
1345 01Apr2017 Y G
1345 01May2017 Y G
;

proc sort data = fake_data out=fake_data_nodupkey nodupkey; by userID event_date; run;
data flag_vars;
set fake_data_nodupkey;
by userID event_date;
if first.userID then do;
	prev_response = response;
	prev_code = code;
	end;
else do;
	prev_response = lag(response);
	prev_code = lag(code);
	end;

response_change = 0;
if response ne prev_response then response_change = 1;

code_change = 0;
if code ne prev_code then code_change = 1;

run;

proc print data=flag_vars;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="results.PNG" style="width: 986px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96627i7A10CF4D2CE39DB0/image-size/large?v=v2&amp;amp;px=999" role="button" title="results.PNG" alt="results.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2024 01:51:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/track-changes-over-time/m-p/928967#M365536</guid>
      <dc:creator>axescot78</dc:creator>
      <dc:date>2024-05-20T01:51:18Z</dc:date>
    </item>
    <item>
      <title>Re: track changes over time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/track-changes-over-time/m-p/928974#M365541</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data flag_vars;
set fake_data_nodupkey;
by userID ;

prev_response = lag(response);
prev_code = lag(code);

if first.userID then do;
	prev_response = response;
	prev_code = code;
	end;

response_change = 0;
if response ne prev_response then response_change = 1;

code_change = 0;
if code ne prev_code then code_change = 1;

 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 May 2024 03:48:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/track-changes-over-time/m-p/928974#M365541</guid>
      <dc:creator>axescot78</dc:creator>
      <dc:date>2024-05-20T03:48:47Z</dc:date>
    </item>
  </channel>
</rss>

