<?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: Adding  a variable to determine whether a postal code changes in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-to-determine-whether-a-postal-code-changes/m-p/390216#M277606</link>
    <description>&lt;P&gt;I'd go about it slightly different:&lt;/P&gt;
&lt;P&gt;(make sure that dataset is sorted by patient_id)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patient_id post_code $;
cards;
1 M4R
1 M4R
1 M4R
2 M4T
2 M4X
2 M4T
;
run;

data want;
set have;
by patient_id;
retain code_change;
if post_code ne lag(post_code) then code_change = 1;
if first.patient_id then code_change = 0;
if last.patient_id then output;
keep patient_id code_change;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;patient_     code_
   id       change

    1          0  
    2          1  
&lt;/PRE&gt;
&lt;P&gt;Note how I presented your example data in a simple data step. Please do so in the future.&lt;/P&gt;</description>
    <pubDate>Wed, 23 Aug 2017 12:56:38 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-08-23T12:56:38Z</dc:date>
    <item>
      <title>Adding  a variable to determine whether a postal code changes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-to-determine-whether-a-postal-code-changes/m-p/390203#M277604</link>
      <description>&lt;P&gt;Hi All,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I feel like this is a pretty straightforward one, but I am still pretty new at data steps, so I thought I would call for backup.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the situation.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a rather large dataset that has multiple records for about 80,000 individuals. Each idividual has anywhere from 1-15 different records associated with their ID. What I'm trying to determine is how often postal code changes among people with multiple records&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ideally the output would be a table with one record per ID with an indicator variable telling me if all the postal codes for a person are the same, or if there is one that is different.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for example, I would like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PATIENT ID &amp;nbsp; &amp;nbsp; &amp;nbsp;POSTAL CODE&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;M4R&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;M4R&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;M4R&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;M4T&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;M4X&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;M4T&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to turn into this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PATIENT ID &amp;nbsp; &amp;nbsp; POSTAL CODE CHANGE&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;'NO' or 0 &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;'YES' or 1&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I assume the first step would be to sort by patient ID and by Postal Code, followed by so sort of "if"/ "then" statement, but I'm having trouble sorting out exactly how that statement should look.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Any thougths would be much appreciated!&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks so much.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Aug 2017 12:40:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-to-determine-whether-a-postal-code-changes/m-p/390203#M277604</guid>
      <dc:creator>righcoastmike</dc:creator>
      <dc:date>2017-08-23T12:40:19Z</dc:date>
    </item>
    <item>
      <title>Re: Adding  a variable to determine whether a postal code changes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-to-determine-whether-a-postal-code-changes/m-p/390206#M277605</link>
      <description>&lt;P&gt;Here's a data step version of such an analysis, I could also imagine that you could write this in PROC SQL&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/* UNTESTED CODE */

data want;
    set have;
    by patient_id; /* Assumes data set have is sorted by patient_id */
    if first.patient_id then count=0;
    prev_postal_code=lag(postal_code);
    if prev_postal_code^=postal_code and not first.patient_id then count+1;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 Aug 2017 12:51:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-to-determine-whether-a-postal-code-changes/m-p/390206#M277605</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-08-23T12:51:52Z</dc:date>
    </item>
    <item>
      <title>Re: Adding  a variable to determine whether a postal code changes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-to-determine-whether-a-postal-code-changes/m-p/390216#M277606</link>
      <description>&lt;P&gt;I'd go about it slightly different:&lt;/P&gt;
&lt;P&gt;(make sure that dataset is sorted by patient_id)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patient_id post_code $;
cards;
1 M4R
1 M4R
1 M4R
2 M4T
2 M4X
2 M4T
;
run;

data want;
set have;
by patient_id;
retain code_change;
if post_code ne lag(post_code) then code_change = 1;
if first.patient_id then code_change = 0;
if last.patient_id then output;
keep patient_id code_change;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;patient_     code_
   id       change

    1          0  
    2          1  
&lt;/PRE&gt;
&lt;P&gt;Note how I presented your example data in a simple data step. Please do so in the future.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Aug 2017 12:56:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-to-determine-whether-a-postal-code-changes/m-p/390216#M277606</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-08-23T12:56:38Z</dc:date>
    </item>
    <item>
      <title>Re: Adding  a variable to determine whether a postal code changes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-to-determine-whether-a-postal-code-changes/m-p/390217#M277607</link>
      <description>&lt;P&gt;You could squeeze just a bit more information out of the data in this way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; by patient_id postal_code;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by patient_id postal_code;&lt;/P&gt;
&lt;P&gt;if first.patient_id then count=0;&lt;/P&gt;
&lt;P&gt;if last.postal_code then count + 1;&lt;/P&gt;
&lt;P&gt;if last.patient_id;&lt;/P&gt;
&lt;P&gt;keep patient_id count;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That way, you get more than a yes/no, you get a count of how many postal codes exist for each patient.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Aug 2017 12:57:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-to-determine-whether-a-postal-code-changes/m-p/390217#M277607</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-08-23T12:57:02Z</dc:date>
    </item>
    <item>
      <title>Re: Adding  a variable to determine whether a postal code changes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-to-determine-whether-a-postal-code-changes/m-p/390231#M277608</link>
      <description>&lt;PRE&gt;
data have;
input patient_id post_code $;
cards;
1 M4R
1 M4R
1 M4R
2 M4T
2 M4X
2 M4T
;
run;
proc sql;
select  patient_id,case when count(distinct post_code)=1 then 0 else 1 end as flag
 from have 
  group by  patient_id;
quit;


&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 Aug 2017 13:39:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-to-determine-whether-a-postal-code-changes/m-p/390231#M277608</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-08-23T13:39:34Z</dc:date>
    </item>
    <item>
      <title>Re: Adding  a variable to determine whether a postal code changes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-to-determine-whether-a-postal-code-changes/m-p/390311#M277609</link>
      <description>&lt;P&gt;Thanks so much everyone. It's crazy to see how many different ways there are to get to the same output.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, thanks for the heads up about how to present data in the future. I can see how that would make life much easier for people offering solutions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The help of this community is always much appreciated!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Mike&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Aug 2017 15:53:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-to-determine-whether-a-postal-code-changes/m-p/390311#M277609</guid>
      <dc:creator>righcoastmike</dc:creator>
      <dc:date>2017-08-23T15:53:15Z</dc:date>
    </item>
  </channel>
</rss>

