<?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: Compare Observations and Assign Value to a New Variable. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compare-Observations-and-Assign-Value-to-a-New-Variable/m-p/229518#M41544</link>
    <description>&lt;P&gt;Below code works with your sample data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dsd dlm=',' truncover;
  input Match_ID $ Comments :$15. Date :ddmmyy10.;
  format date date9.;
  datalines;
1,Market Only
1,Different
1,Different
2,Market Only
2,Different
3,Same,10-07-2015
3,Same,11-08-2015
3,Same,12-09-2015
;
run;

proc format;
  value $comments (default=1)
    'Market Only' = 'A'
    'Different'   = 'B'
    'Same'        = 'C'
  ;
run;

data inter;
  set have;
  _com=put(Comments,$comments.);
run;

proc transpose data=inter out=inter(drop=_name_) prefix=_comment;
  by Match_ID;
  var _com;
run;

data want(drop=_:);
  merge have inter;
  by Match_id;
  array com {*} _comment:;
  if last.match_id then
    do;
      call sortc(of com[*]);
      _string=cats(of com[*]);
      if prxmatch('/^A+B$/',strip(_string))&amp;gt;0 then Survivor='Y';
      if prxmatch('/^C+$/',strip(_string))&amp;gt;0 then Survivor='Y';
    end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;True if the string starts with one or several 'A' and ends with exactly one 'B'&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if prxmatch('/^A+B$/',strip(_string))&amp;gt;0 then Survivor='Y';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;True if the string&amp;nbsp;only contains one or several 'C'&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if prxmatch('/^C+$/',strip(_string))&amp;gt;0 then Survivor='Y';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 12 Oct 2015 10:48:20 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2015-10-12T10:48:20Z</dc:date>
    <item>
      <title>Compare Observations and Assign Value to a New Variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-Observations-and-Assign-Value-to-a-New-Variable/m-p/229508#M41542</link>
      <description>&lt;P&gt;I have the following dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Match ID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Comments &amp;nbsp; &amp;nbsp;Date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Survivor&lt;/P&gt;
&lt;P&gt;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; &amp;nbsp; &amp;nbsp; &amp;nbsp; Market Only&lt;/P&gt;
&lt;P&gt;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; &amp;nbsp; &amp;nbsp; &amp;nbsp; Different&lt;/P&gt;
&lt;P&gt;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; &amp;nbsp; &amp;nbsp; &amp;nbsp; Different&lt;/P&gt;
&lt;P&gt;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; &amp;nbsp; &amp;nbsp; &amp;nbsp;Market Only&lt;/P&gt;
&lt;P&gt;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; &amp;nbsp; &amp;nbsp; &amp;nbsp;Different&lt;/P&gt;
&lt;P&gt;3 &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; Same &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10-07-2015&lt;/P&gt;
&lt;P&gt;3 &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Same &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11-08-2015&lt;/P&gt;
&lt;P&gt;3 &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Same &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12-09-2015&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I need to do is compare the observations within a given Match ID like follows:&lt;/P&gt;
&lt;P&gt;- If there are two 'different' observations within a given match id, then Survivor=' ' (eg. where match id is 1)&lt;/P&gt;
&lt;P&gt;-if there is one 'different' observation and others are 'market only' , then Survior='Y' for the case where it is 'different'&lt;/P&gt;
&lt;P&gt;-if all are 'same' within a given match id, then survivor='Y' for the case with latest created date. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Essentially, the output should be as follows:&lt;/P&gt;
&lt;P&gt;Match ID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Comments &amp;nbsp; &amp;nbsp;Date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Survivor&lt;/P&gt;
&lt;P&gt;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; &amp;nbsp; &amp;nbsp; &amp;nbsp; Market Only&lt;/P&gt;
&lt;P&gt;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; &amp;nbsp; &amp;nbsp; &amp;nbsp; Different&lt;/P&gt;
&lt;P&gt;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; &amp;nbsp; &amp;nbsp; &amp;nbsp; Different&lt;/P&gt;
&lt;P&gt;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; &amp;nbsp; &amp;nbsp; &amp;nbsp;Market Only&lt;/P&gt;
&lt;P&gt;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; &amp;nbsp; &amp;nbsp; &amp;nbsp;Different &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; 'Y'&lt;/P&gt;
&lt;P&gt;3 &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; Same &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10-07-2015&lt;/P&gt;
&lt;P&gt;3 &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Same &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11-08-2015&lt;/P&gt;
&lt;P&gt;3 &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Same &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12-09-2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;'Y'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help is greatly appreciated. Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Oct 2015 09:20:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-Observations-and-Assign-Value-to-a-New-Variable/m-p/229508#M41542</guid>
      <dc:creator>sasmaverick</dc:creator>
      <dc:date>2015-10-12T09:20:08Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Observations and Assign Value to a New Variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-Observations-and-Assign-Value-to-a-New-Variable/m-p/229518#M41544</link>
      <description>&lt;P&gt;Below code works with your sample data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dsd dlm=',' truncover;
  input Match_ID $ Comments :$15. Date :ddmmyy10.;
  format date date9.;
  datalines;
1,Market Only
1,Different
1,Different
2,Market Only
2,Different
3,Same,10-07-2015
3,Same,11-08-2015
3,Same,12-09-2015
;
run;

proc format;
  value $comments (default=1)
    'Market Only' = 'A'
    'Different'   = 'B'
    'Same'        = 'C'
  ;
run;

data inter;
  set have;
  _com=put(Comments,$comments.);
run;

proc transpose data=inter out=inter(drop=_name_) prefix=_comment;
  by Match_ID;
  var _com;
run;

data want(drop=_:);
  merge have inter;
  by Match_id;
  array com {*} _comment:;
  if last.match_id then
    do;
      call sortc(of com[*]);
      _string=cats(of com[*]);
      if prxmatch('/^A+B$/',strip(_string))&amp;gt;0 then Survivor='Y';
      if prxmatch('/^C+$/',strip(_string))&amp;gt;0 then Survivor='Y';
    end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;True if the string starts with one or several 'A' and ends with exactly one 'B'&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if prxmatch('/^A+B$/',strip(_string))&amp;gt;0 then Survivor='Y';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;True if the string&amp;nbsp;only contains one or several 'C'&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if prxmatch('/^C+$/',strip(_string))&amp;gt;0 then Survivor='Y';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Oct 2015 10:48:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-Observations-and-Assign-Value-to-a-New-Variable/m-p/229518#M41544</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2015-10-12T10:48:20Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Observations and Assign Value to a New Variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-Observations-and-Assign-Value-to-a-New-Variable/m-p/229519#M41545</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
infile cards missover;
  input MATCH_ID   1.    COMMENTS $12.   DATE    mmddyy10.    ;
  format DATE date9.;
  cards;
1 Market Only
1 Different
1 Different
2 Market Only
2 Different
3 Same          10-07-2015
3 Same          11-08-2015
3 Same          12-09-2015
run;
proc sort ;
  by  MATCH_ID descending COMMENTS DATE;
data WANT; 
  set HAVE;
  by  MATCH_ID;
  if first.MATCH_ID then call missing(NB_DIFFERENT, NB_SAME, NB_ID);
  if COMMENTS='Different' then NB_DIFFERENT+1;
  if COMMENTS='Same'      then NB_SAME     +1;
  NB_ID+1;
  if last.MATCH_ID then do;
    if NB_DIFFERENT=1 then SURVIVOR='Y';
    if NB_SAME=NB_ID  then SURVIVOR='Y';
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Oct 2015 11:01:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-Observations-and-Assign-Value-to-a-New-Variable/m-p/229519#M41545</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2015-10-12T11:01:52Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Observations and Assign Value to a New Variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-Observations-and-Assign-Value-to-a-New-Variable/m-p/229525#M41549</link>
      <description>Thank you very much! Works mostly.</description>
      <pubDate>Mon, 12 Oct 2015 11:47:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-Observations-and-Assign-Value-to-a-New-Variable/m-p/229525#M41549</guid>
      <dc:creator>sasmaverick</dc:creator>
      <dc:date>2015-10-12T11:47:52Z</dc:date>
    </item>
  </channel>
</rss>

