<?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: Conditionally compare values within one variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-compare-values-within-one-variable/m-p/705344#M216350</link>
    <description>&lt;P&gt;You cannot use FIRST. and LAST. without first listing those variables in a BY statement.&lt;/P&gt;
&lt;P&gt;To get unique combinations of three variables use this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by PatientID Stage Value;
  if first.value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 11 Dec 2020 17:57:17 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-12-11T17:57:17Z</dc:date>
    <item>
      <title>Conditionally compare values within one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-compare-values-within-one-variable/m-p/705336#M216346</link>
      <description>&lt;P&gt;I'm parsing down a dataset for final output. Here are the datalines:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input PatientID Stage Var1;
datalines;
1001 1 0
1001 2 0
1001 2 1
1002 2 1
1005 3 3
1006 2 1
1010 3 0
1010 3 2
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What I'm trying to do is if there are records of the same PatientID (1001) where the Stages don't match, but Var1 matches, delete both records. If the there are records of the same PatientID where Stage matches and Var1 doesn't match, I want to keep both records. Here is some code that I've tried:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if first.PatientID=last.PatientID and first.Stage NE last.Stage and first.Var1=last.Var1 then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And here is what the log read.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;NOTE: Variable first.PatientID is uninitialized.
NOTE: Variable last.PatientID is uninitialized.
NOTE: Variable first.Stage is uninitialized.
NOTE: Variable last.Stage is uninitialized.
NOTE: Variable first.Var1 is uninitialized.
NOTE: Variable last.Var1 is uninitialized.
NOTE: There were 8 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 8 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds&lt;/PRE&gt;
&lt;P&gt;Is this even possible to do? I tried using a proc sql as well but ran into the same issue of trying to compare values within the same column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Dec 2020 17:54:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-compare-values-within-one-variable/m-p/705336#M216346</guid>
      <dc:creator>analyticalepi</dc:creator>
      <dc:date>2020-12-11T17:54:12Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally compare values within one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-compare-values-within-one-variable/m-p/705344#M216350</link>
      <description>&lt;P&gt;You cannot use FIRST. and LAST. without first listing those variables in a BY statement.&lt;/P&gt;
&lt;P&gt;To get unique combinations of three variables use this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by PatientID Stage Value;
  if first.value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Dec 2020 17:57:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-compare-values-within-one-variable/m-p/705344#M216350</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-11T17:57:17Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally compare values within one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-compare-values-within-one-variable/m-p/705394#M216382</link>
      <description>&lt;P&gt;You want to detect and delete all; cases (within a patientid) with unchanging var1, but changing stage:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input PatientID Stage Var1;
seq+1;
datalines;
1001 1 0
1001 2 0
1001 2 1
1002 2 1
1005 3 3
1006 2 1
1010 3 0
1010 3 2
;

data want;
  set have;
  by patientid var1 stage notsorted;
  if ???? then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Using the&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;by pateindid var1 stage notsorted;&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;has patientid as the major grouping key, stage as the minor key, and var1 as the middle key.&amp;nbsp; SAS honors this hierarchy, such that when a given key changes (setting its&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;first.&lt;/STRONG&gt;&lt;/EM&gt; indicator, it also sets the indicators for all keys to its right).&amp;nbsp; BTW, notsorted means that, for a given patientid/var1 the stage values can go up or down - SAS need not expect either ascending or descending order in identifying groups, for any of the "by" keys.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This means that if you encounter a record that starts a new stage value (first.stage=1), but does not at the same time start a new var1 value, then it should be deleted.&amp;nbsp; Similarly if it ends the current value of stage (last.stage=1) but does not end the current value of var1, then it should also be deleted.&amp;nbsp; Build the expression to test for those conditions, and put it in as a replacement for the ???? above.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Dec 2020 20:54:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-compare-values-within-one-variable/m-p/705394#M216382</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-12-11T20:54:12Z</dc:date>
    </item>
  </channel>
</rss>

