<?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: Trying to condition on when a variable changes from 0 to 1 or 1 to 0 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-condition-on-when-a-variable-changes-from-0-to-1-or-1/m-p/685093#M207725</link>
    <description>This helped me visualize the data, I appreciate the help. An issue I ran&lt;BR /&gt;into when checking this, though, is that some properties which changed in&lt;BR /&gt;their second year and changed again after that only shows as changing once&lt;BR /&gt;in the new "pattern" variable. For example, one property started with 1,&lt;BR /&gt;switched to 0 for two years, then back to 1. The new variable only shows&lt;BR /&gt;"1 0".&lt;BR /&gt;</description>
    <pubDate>Fri, 18 Sep 2020 20:22:08 GMT</pubDate>
    <dc:creator>jss539</dc:creator>
    <dc:date>2020-09-18T20:22:08Z</dc:date>
    <item>
      <title>Trying to condition on when a variable changes from 0 to 1 or 1 to 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-condition-on-when-a-variable-changes-from-0-to-1-or-1/m-p/684985#M207686</link>
      <description>&lt;P&gt;I have a dataset on properties, where each property has several years of observations.&amp;nbsp; With that, each property is assigned as either independent or branded.&amp;nbsp; Some properties, though, switched from independent to branded (or vice versa) in one of their years.&amp;nbsp; Right now, I have a "branded" variable that is either 0 or 1.&amp;nbsp;For example, property 1 could have data from 2002 to 2011 and be branded (branded=1) in all of the years.&amp;nbsp; Property 2 could have data from 2005 to 2012 and be independent (branded=0) for all the years.&amp;nbsp; Property 3, which is the one I would be interested in, could be branded&amp;nbsp;(branded=1) from 2000 to 2009, then independent&amp;nbsp;(branded=0) from 2010 and on.&amp;nbsp; My end goal is to create 3 more summy variables: 1 that tells if the property switched, 1 that is tells is it switched from branded to independent, and another that tells if it switched from independent to branded.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The variables I have to work with are: propertyID, address, and branded.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I've tried so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First, I successfully made a new dataset, the one I'm using below, that is only of hotels that changed.&amp;nbsp; I just need to see which year and which way they changed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sort data=changed_props;
by address year;
run;

/*This is where I'm running into an issue.&amp;nbsp; I am only looking for hotels that changed from branded=0 to branded=1.
What I want is to tell when they changed either way.&amp;nbsp; I thought about doing 2 of these blocks of code and merging them,
But I worry that I'll be getting the first year of data for each property rather than just the year it changed*/&lt;BR /&gt;
data changed_props_temp;
set changed_props;
by address;
where branded&amp;gt;0;
if first.address;
year_changed = year;
change_year = 1;
keep address year_changed change_year branded;
run;

*Putting it back;
data changed_props;
merge changed_props changed_props_temp;
by address;
if year = year_changed then change_year=1;
else change_year=0;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In all, what I need is to tell when the branded variable switches from 0 to 1 or 1 to 0 from the past observation with the same address.&amp;nbsp; Thanks for the help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 14:27:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-condition-on-when-a-variable-changes-from-0-to-1-or-1/m-p/684985#M207686</guid>
      <dc:creator>jss539</dc:creator>
      <dc:date>2020-09-18T14:27:36Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to condition on when a variable changes from 0 to 1 or 1 to 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-condition-on-when-a-variable-changes-from-0-to-1-or-1/m-p/685029#M207703</link>
      <description>&lt;P&gt;Is there any possibility that a property will switch (say from 0 to 1) and then later switch back (say from 1 to 0)?&amp;nbsp; What would you like the result to be for that case?&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 16:22:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-condition-on-when-a-variable-changes-from-0-to-1-or-1/m-p/685029#M207703</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-09-18T16:22:45Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to condition on when a variable changes from 0 to 1 or 1 to 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-condition-on-when-a-variable-changes-from-0-to-1-or-1/m-p/685037#M207708</link>
      <description>Good question. At first I did not think this was possible. But, looking&lt;BR /&gt;at the data, there are a few instances of it. I would prefer to be able to&lt;BR /&gt;have a result for that as well, but I could go through manually if I needed&lt;BR /&gt;to.&lt;BR /&gt;</description>
      <pubDate>Fri, 18 Sep 2020 17:04:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-condition-on-when-a-variable-changes-from-0-to-1-or-1/m-p/685037#M207708</guid>
      <dc:creator>jss539</dc:creator>
      <dc:date>2020-09-18T17:04:08Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to condition on when a variable changes from 0 to 1 or 1 to 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-condition-on-when-a-variable-changes-from-0-to-1-or-1/m-p/685078#M207723</link>
      <description>&lt;P&gt;Given that other possibilities exist, I would just try to find the patterns.&amp;nbsp; For example after sorting your data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data changed_props_temp;
set changed_props;
by address branded notsorted;
length pattern $ 9;&lt;BR /&gt;if first.address then pattern = ' ';
if first.branded then pattern = catx(' ', pattern, branded);
if last.address;
retain pattern;
keep address pattern;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Not sure if you want to keep more variables, but for now this tells you what patterns of branded exist over time.&amp;nbsp; You might just run a PROC FREQ on PATTERN after this to see what patterns you will need to plan for.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 19:51:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-condition-on-when-a-variable-changes-from-0-to-1-or-1/m-p/685078#M207723</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-09-18T19:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to condition on when a variable changes from 0 to 1 or 1 to 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-condition-on-when-a-variable-changes-from-0-to-1-or-1/m-p/685093#M207725</link>
      <description>This helped me visualize the data, I appreciate the help. An issue I ran&lt;BR /&gt;into when checking this, though, is that some properties which changed in&lt;BR /&gt;their second year and changed again after that only shows as changing once&lt;BR /&gt;in the new "pattern" variable. For example, one property started with 1,&lt;BR /&gt;switched to 0 for two years, then back to 1. The new variable only shows&lt;BR /&gt;"1 0".&lt;BR /&gt;</description>
      <pubDate>Fri, 18 Sep 2020 20:22:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-condition-on-when-a-variable-changes-from-0-to-1-or-1/m-p/685093#M207725</guid>
      <dc:creator>jss539</dc:creator>
      <dc:date>2020-09-18T20:22:08Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to condition on when a variable changes from 0 to 1 or 1 to 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-condition-on-when-a-variable-changes-from-0-to-1-or-1/m-p/685137#M207750</link>
      <description>&lt;P&gt;That shouldn't happen.&amp;nbsp; Please post the log from that DATA step so I can verify the logic that it contains.&lt;/P&gt;</description>
      <pubDate>Sat, 19 Sep 2020 03:03:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-condition-on-when-a-variable-changes-from-0-to-1-or-1/m-p/685137#M207750</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-09-19T03:03:49Z</dc:date>
    </item>
  </channel>
</rss>

