<?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 Create identifier by ID variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680204#M205516</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the following have data where I only care about codes a, b, and c. I have an identifier variable for each of the codes that are 1 or 0 for whether that data row has the specific code. However, I'd like that identifier variable for each code to be 1 for every row of the ID if that ID had the code at some point. I've included the want data below. Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id $ code $ code_a code_b code_c ;
   datalines;
1 a 1 0 0
1 b 0 1 0
1 c 0 0 1
2 b 0 1 0
2 f 0 0 0
3 a 1 0 0
3 c 0 0 1
;
run;

data want;
   input id $ code $ code_a code_b code_c ;
   datalines;
1 a 1 1 1
1 b 1 1 1
1 c 1 1 1
2 b 0 1 0
2 f 0 1 0
3 a 1 0 1
3 c 1 0 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 29 Aug 2020 14:32:45 GMT</pubDate>
    <dc:creator>PeterBr</dc:creator>
    <dc:date>2020-08-29T14:32:45Z</dc:date>
    <item>
      <title>Create identifier by ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680204#M205516</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the following have data where I only care about codes a, b, and c. I have an identifier variable for each of the codes that are 1 or 0 for whether that data row has the specific code. However, I'd like that identifier variable for each code to be 1 for every row of the ID if that ID had the code at some point. I've included the want data below. Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id $ code $ code_a code_b code_c ;
   datalines;
1 a 1 0 0
1 b 0 1 0
1 c 0 0 1
2 b 0 1 0
2 f 0 0 0
3 a 1 0 0
3 c 0 0 1
;
run;

data want;
   input id $ code $ code_a code_b code_c ;
   datalines;
1 a 1 1 1
1 b 1 1 1
1 c 1 1 1
2 b 0 1 0
2 f 0 1 0
3 a 1 0 1
3 c 1 0 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 29 Aug 2020 14:32:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680204#M205516</guid>
      <dc:creator>PeterBr</dc:creator>
      <dc:date>2020-08-29T14:32:45Z</dc:date>
    </item>
    <item>
      <title>Re: Create identifier by ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680205#M205517</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
class id;
var code_:;
output
  out=want (drop=_type_ _freq_)
  max()=
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 29 Aug 2020 14:41:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680205#M205517</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-29T14:41:02Z</dc:date>
    </item>
    <item>
      <title>Re: Create identifier by ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680206#M205518</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id $ code $ code_a code_b code_c ;
   datalines;
1 a 1 0 0
1 b 0 1 0
1 c 0 0 1
2 b 0 1 0
2 f 0 0 0
3 a 1 0 0
3 c 0 0 1
;

data want(drop=a b c);
   do until (last.id);
      set have;
      by id;
      a = max(code_a, a);
      b = max(code_b, b);
      c = max(code_c, c);
   end;

   do until (last.id);
      set have;
      by id;
      code_a = a;
      code_b = b;
      code_c = c;
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 29 Aug 2020 14:44:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680206#M205518</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-08-29T14:44:20Z</dc:date>
    </item>
    <item>
      <title>Re: Create identifier by ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680207#M205519</link>
      <description>&lt;P&gt;Here is a program that takes advantage of how the sas data step populates a PDV (program data vector, think of it as a list of variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The strategy below does the following:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Merges HAVE with 3 subsets of Have with matching ID's
&lt;OL&gt;
&lt;LI&gt;subset with code_a=1, and with 2 variables (id and code_a).&lt;/LI&gt;
&lt;LI&gt;subset with code_b=1, and with 2 variables (id and code_b).&lt;/LI&gt;
&lt;LI&gt;subset with code_c=1, and with 2 variables (id and code_c).&lt;BR /&gt;The very first observation for each id will have a pattern of 1's and 0's for the codes the you want, but subsequent observations produced by merge would not.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;With the desired codes in hand (i.e. when the record-in-hand is the first produced by merge), read in all the other records for the id (except the code variables) and output the records.&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id $ code $ code_a code_b code_c ;
   datalines;
1 a 1 0 0
1 b 0 1 0
1 c 0 0 1
2 b 0 1 0
2 f 0 0 0
3 a 1 0 0
3 c 0 0 1
;
run;

data want;
  merge have
        have (keep=id code_a where=(code_a=1))
        have (keep=id code_b where=(code_b=1))
        have (keep=id code_c where=(code_c=1)) ;
  by id;
  if first.id then do until (last.id);
    set have (drop=code_a code_b code_c);
	by id;
	output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Ordinarily I would suggest using the proc summary solutioni offered by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; , but it produces one record per id, which you would have to re-merge with have if you want the original number of records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And this program may look a little strange compared to the well-known double do offered by draycut.&amp;nbsp; I'd suggest sticking with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;'s suggestion.&amp;nbsp; But if you need to scale to situations with multiple variables for each code&amp;nbsp; (e.g.&amp;nbsp; code_a1&amp;nbsp;code_a2 code_a3 code_a4 code_a5, code_b1&amp;nbsp;code_b2 code_b3 code_b4 code_b5,&amp;nbsp; code_c1&amp;nbsp;code_c2 code_c3 code_c4 code_c5 ), then the code above might be easier to maintain.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Aug 2020 20:34:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680207#M205519</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-08-29T20:34:48Z</dc:date>
    </item>
    <item>
      <title>Re: Create identifier by ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680211#M205523</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/321225"&gt;@PeterBr&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
   input id $ code $ code_a code_b code_c ;
   datalines;
1 a 1 0 0
1 b 0 1 0
1 c 0 0 1
2 b 0 1 0
2 f 0 0 0
3 a 1 0 0
3 c 0 0 1
;
run;

data want;
 do _iorc_=1 by 1 until(last.id);
  set have;
  by id;
  array t  code_a code_b code_c ;
  array u(3) _temporary_;
  do _n_=1 to dim(t);
   if t(_n_) then u(_n_)=t(_n_);
  end;
 end;
 do _iorc_=1 to _iorc_;
  set have;
  do _n_=1 to dim(t);
   t(_n_)=^^u(_n_);
  end;
  output;
 end;
 call missing(of u(*));
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 29 Aug 2020 15:31:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680211#M205523</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-08-29T15:31:41Z</dc:date>
    </item>
    <item>
      <title>Re: Create identifier by ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680239#M205539</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;very nice.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Aug 2020 18:42:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680239#M205539</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-08-29T18:42:59Z</dc:date>
    </item>
    <item>
      <title>Re: Create identifier by ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680280#M205555</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id $ code $ code_a code_b code_c ;
   datalines;
1 a 1 0 0
1 b 0 1 0
1 c 0 0 1
2 b 0 1 0
2 f 0 0 0
3 a 1 0 0
3 c 0 0 1
;
run;
proc sql;
create table want as
select id,code,
max(code_a) as code_a,
max(code_b) as code_b,
max(code_c) as code_c
from have 
 group by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 30 Aug 2020 10:45:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-identifier-by-ID-variable/m-p/680280#M205555</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-08-30T10:45:09Z</dc:date>
    </item>
  </channel>
</rss>

