<?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: Flag if there is one of each of 3 pre-specified values of a variable within a by  group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648092#M194064</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
    table id*sample/out=_counts_ noprint;
run;
proc transpose data=_counts_ out=_counts_t_;
	var count;
	by id;
	id sample;
run;
data want;
	merge have _counts_t_(drop=_:);
	by id;
	if a&amp;gt;=1 and b&amp;gt;=1 and c&amp;gt;=1 then flag=1;
	else flag=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Assumes your input data set is sorted by ID.&lt;/P&gt;</description>
    <pubDate>Fri, 15 May 2020 16:38:00 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-05-15T16:38:00Z</dc:date>
    <item>
      <title>Flag if there is one of each of 3 pre-specified values of a variable within a by  group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648087#M194062</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Dataset below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp;&amp;nbsp; sample&lt;/P&gt;&lt;P&gt;p1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&lt;/P&gt;&lt;P&gt;p1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b&lt;/P&gt;&lt;P&gt;p1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&lt;/P&gt;&lt;P&gt;p1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c&lt;/P&gt;&lt;P&gt;p2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&lt;/P&gt;&lt;P&gt;p2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b&lt;/P&gt;&lt;P&gt;p3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;p3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;p3&amp;nbsp; &amp;nbsp;&amp;nbsp; b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want it to look as follows, where flag =1 if within a by group (ID), there is at least one observation within the sample variable of each of a, b, and c&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp;&amp;nbsp; sample&amp;nbsp; flag&lt;/P&gt;&lt;P&gt;p1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;p1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;p1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;p1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;p2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;p2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;p3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;p3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;p3&amp;nbsp; &amp;nbsp;&amp;nbsp; b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to do this? Maybe with proc sql?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank yoU!&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 16:30:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648087#M194062</guid>
      <dc:creator>KPCklebspn</dc:creator>
      <dc:date>2020-05-15T16:30:00Z</dc:date>
    </item>
    <item>
      <title>Re: Flag if there is one of each of 3 pre-specified values of a variable within a by  group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648090#M194063</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/141326"&gt;@KPCklebspn&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input (ID   sample) ($);
cards;
p1      a
p1      b
p1      a
p1       c
p2      a
p2      b
p3     a        
p3     c        
p3     b       
;

proc sql;
create table want as
select *,count(distinct ifc(sample in ('a','b','c'),sample,' '))=3 as Flag
from have
group by id;
quit;

proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 May 2020 16:37:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648090#M194063</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-15T16:37:03Z</dc:date>
    </item>
    <item>
      <title>Re: Flag if there is one of each of 3 pre-specified values of a variable within a by  group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648092#M194064</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
    table id*sample/out=_counts_ noprint;
run;
proc transpose data=_counts_ out=_counts_t_;
	var count;
	by id;
	id sample;
run;
data want;
	merge have _counts_t_(drop=_:);
	by id;
	if a&amp;gt;=1 and b&amp;gt;=1 and c&amp;gt;=1 then flag=1;
	else flag=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Assumes your input data set is sorted by ID.&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 16:38:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648092#M194064</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-15T16:38:00Z</dc:date>
    </item>
    <item>
      <title>Re: Flag if there is one of each of 3 pre-specified values of a variable within a by  group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648097#M194068</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Double DoW-loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input (ID sample) ($);
cards;
p1 a
p1 b
p1 a
p1 c
p2 a
p2 b
p3 a
p3 c
p3 b
;
run;

data want;
  list = 'a b c'; 
  drop list;
  do _N_ = 1 by 1 until(last.ID);
    set have;
    by ID;
    list = TRANWRD(list, strip(sample), " ");
  end;

  flag = (list = " ");

  do _N_ = 1 to _N_;
    set have;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 16:54:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648097#M194068</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-05-15T16:54:02Z</dc:date>
    </item>
    <item>
      <title>Re: Flag if there is one of each of 3 pre-specified values of a variable within a by  group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648110#M194072</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input (ID   sample) ($);
cards;
p1      a
p1      b
p1      a
p1       c
p2      a
p2      b
p3     a        
p3     c        
p3     b       
;

data want ;
 if _n_=1 then do;
  dcl hash H () ;
  h.definekey  ("sample") ;
  h.definedone () ;
 end;
 do _n_=1 by 1 until(last.id);
  set have;
  by id;
  if h.check()=0 or sample not in ('a','b','c') then continue;
  h.add();
 end;
 do _n_=1 to _n_;
  set have;
  Flag=h.num_items=3;
  output;
 end;
 h.clear();
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 May 2020 17:36:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648110#M194072</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-15T17:36:51Z</dc:date>
    </item>
    <item>
      <title>Re: Flag if there is one of each of 3 pre-specified values of a variable within a by  group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648112#M194073</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can be done in a data step but needs to be read twice.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have ;
input ID $   sample $ ;
cards;
p1      a
p1      b
p1      a
p1      c
p2      a
p2      b
p3      a        
p3      c        
p3      b       
;

proc format ;
   invalue samp
     "a" = 1
     "b" = 2
     "c" = 3 
   ;
run ;

data want ;
   array nums (*) a b c ;
   drop a b c ;
   do until (last.id) ;
      set have ;
      by id ;
      nums(input(sample,samp.))=1 ;
   end ;
   flag = sum(of nums(*))=3 ;
   do until (last.id) ;
      set have ;
      by id ;
      output ;
   end ; 
run ;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 17:39:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648112#M194073</guid>
      <dc:creator>biopharma</dc:creator>
      <dc:date>2020-05-15T17:39:47Z</dc:date>
    </item>
    <item>
      <title>Re: Flag if there is one of each of 3 pre-specified values of a variable within a by  group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648114#M194074</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; - elegant!</description>
      <pubDate>Fri, 15 May 2020 17:41:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648114#M194074</guid>
      <dc:creator>biopharma</dc:creator>
      <dc:date>2020-05-15T17:41:54Z</dc:date>
    </item>
    <item>
      <title>Re: Flag if there is one of each of 3 pre-specified values of a variable within a by  group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648118#M194076</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/254033"&gt;@biopharma&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 17:53:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648118#M194076</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-15T17:53:53Z</dc:date>
    </item>
    <item>
      <title>Re: Flag if there is one of each of 3 pre-specified values of a variable within a by  group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648119#M194077</link>
      <description>DoW-loop + hash = perfect match. &lt;BR /&gt;&lt;BR /&gt;Maybe just `if sample in ('a','b','c') then h.replace();` ? &lt;BR /&gt;&lt;BR /&gt;Bart&lt;BR /&gt;</description>
      <pubDate>Fri, 15 May 2020 17:54:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648119#M194077</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-05-15T17:54:49Z</dc:date>
    </item>
    <item>
      <title>Re: Flag if there is one of each of 3 pre-specified values of a variable within a by  group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648120#M194078</link>
      <description>&lt;P&gt;"&lt;SPAN&gt;Maybe just `if sample in ('a','b','c') then h.replace();` ?&amp;nbsp;" - I agree Sir!&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 17:56:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-if-there-is-one-of-each-of-3-pre-specified-values-of-a/m-p/648120#M194078</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-15T17:56:13Z</dc:date>
    </item>
  </channel>
</rss>

