<?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: Creating an indicator variable based on whether a row exists in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450619#M113472</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input client code $;
cards;
1 A
1 B
2 A
3 A
3 C
3 D
4 A
5 A
5 B
;
run;
proc sql;
select client,'A' as code,
 sum(code='B') as B,sum(code='C') as C,sum(code='D') as D 
  from have
   group by client;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 03 Apr 2018 13:21:17 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2018-04-03T13:21:17Z</dc:date>
    <item>
      <title>Creating an indicator variable based on whether a row exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450584#M113455</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a specific question that is not easy to google. Most certainly someone has already asked it, so apologies for asking it twice.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset that looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Client Code&lt;/P&gt;&lt;P&gt;1 A&lt;/P&gt;&lt;P&gt;1 B&lt;/P&gt;&lt;P&gt;2 A&lt;/P&gt;&lt;P&gt;3 A&lt;/P&gt;&lt;P&gt;3 C&lt;/P&gt;&lt;P&gt;3 D&lt;/P&gt;&lt;P&gt;4 A&lt;/P&gt;&lt;P&gt;5 A&lt;/P&gt;&lt;P&gt;5 B&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For every Client number I have the code A, so I want to make my dataset a little smaller and easier to handle by creating indicators if a client also has the codes B, C and/or D.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, my final dataset (after removing the redundant rows) looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Client Code ind_B ind_C ind_D&lt;/P&gt;&lt;P&gt;1 A 1 0 0&lt;/P&gt;&lt;P&gt;2 A 0 0 0&lt;/P&gt;&lt;P&gt;3 A 0 1 1&lt;/P&gt;&lt;P&gt;4 A&amp;nbsp;0 0&amp;nbsp;0&lt;/P&gt;&lt;P&gt;5 A 1 0 0&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I succeeded to do this by creating 2 datasets and joining them, but that is not time and memory efficient. I prefer not to loop over all clients and I'm convinced there is a better way to do this, I just cannot come up with it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me?&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 12:17:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450584#M113455</guid>
      <dc:creator>Robs234</dc:creator>
      <dc:date>2018-04-03T12:17:25Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an indicator variable based on whether a row exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450586#M113457</link>
      <description>&lt;P&gt;TBH I would strongly suspect that your want dataset here would be larger than the have dataset.&amp;nbsp; In the have dataset you are only capturing data where it appears, in the want data you are creating empty data items where there is no data.&amp;nbsp; For example:&lt;BR /&gt;Client 1 has two datapoints, a and b, but in the want dataset the same client has 3 datapoints, b, c, and d two of which are redundant.&amp;nbsp; Unless there is a very good reason (and I can't see one) to have that want structure, I would stick with your first, and filter out where code="A" if they all have it, as that is not needed at all.&amp;nbsp; This would effecively shrink your have to 4*2 fields which is far smaller than your want of 5*5.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 12:22:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450586#M113457</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-03T12:22:50Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an indicator variable based on whether a row exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450595#M113461</link>
      <description>&lt;P&gt;You are right in my example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did not mention though that each row contains much more columns with client information.&lt;/P&gt;&lt;P&gt;These columns are independent of the code, so the dataset contains a lot of duplicative information.&lt;/P&gt;&lt;P&gt;Therefore it is more efficient to create an indicator such that I can remove the rows that have not code A.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 12:40:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450595#M113461</guid>
      <dc:creator>Robs234</dc:creator>
      <dc:date>2018-04-03T12:40:16Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an indicator variable based on whether a row exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450600#M113464</link>
      <description>&lt;P&gt;See this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input client code $;
cards;
1 A
1 B
2 A
3 A
3 C
3 D
4 A
5 A
5 B
;
run;

data int;
set have (where=(code ne 'A')) ;
val = 1;
run;

proc transpose
  data=int
  out=codes (drop=_name_)
  prefix=ind_
;
by client;
id code;
var val;
run;

data want;
merge
  have (
    in=a
    where=(code = 'A')
  )
  codes
;
by client;
if a;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Apr 2018 12:50:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450600#M113464</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-03T12:50:41Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an indicator variable based on whether a row exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450601#M113465</link>
      <description>&lt;P&gt;Sorry, you going to have to clarify, test data/required output etc.&amp;nbsp; This:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;"I can remove the rows that have not code A"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;To me resolves as:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table want as&lt;BR /&gt;  select *&lt;BR /&gt;  from   have
  where  client not in (select distinct client from have where code="A");
quit;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Ie create a copy of the data with only clients who have an "A" record.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 12:52:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450601#M113465</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-03T12:52:53Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an indicator variable based on whether a row exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450619#M113472</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input client code $;
cards;
1 A
1 B
2 A
3 A
3 C
3 D
4 A
5 A
5 B
;
run;
proc sql;
select client,'A' as code,
 sum(code='B') as B,sum(code='C') as C,sum(code='D') as D 
  from have
   group by client;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Apr 2018 13:21:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450619#M113472</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-04-03T13:21:17Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an indicator variable based on whether a row exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450647#M113489</link>
      <description>Thanks this is exactly what I wanted</description>
      <pubDate>Tue, 03 Apr 2018 14:20:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450647#M113489</guid>
      <dc:creator>Robs234</dc:creator>
      <dc:date>2018-04-03T14:20:10Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an indicator variable based on whether a row exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450649#M113491</link>
      <description>Thank you, this gives the output I needed, although I prefer Ksharp's answer</description>
      <pubDate>Tue, 03 Apr 2018 14:21:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450649#M113491</guid>
      <dc:creator>Robs234</dc:creator>
      <dc:date>2018-04-03T14:21:30Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an indicator variable based on whether a row exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450696#M113513</link>
      <description>&lt;P&gt;Seems like a method works without knowing the&amp;nbsp;values of CODE would be useful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input client code :$1. @@;
   cards;
1 A 1 B 2 A 3 A 3 C 3 D 4 A 5 A 5 B
;;;;
   run;
proc transreg data=have design;
   id client;
   model class(code/zero=none CPREFIX=0);
   output out=design(drop=Int: _:);
   run;
proc print;
   run;
proc means noprint nway;
   class client;
   output out=want(drop=_:) sum=;
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 213px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/19581iA7531295E7CB6BD2/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 15:08:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/450696#M113513</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2018-04-03T15:08:12Z</dc:date>
    </item>
    <item>
      <title>Re: Creating an indicator variable based on whether a row exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/451057#M113655</link>
      <description>&lt;P&gt;John King,&lt;/P&gt;
&lt;P&gt;Long time no see.&lt;/P&gt;
&lt;P&gt;Sure . That would be easy.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) PROC FREQ to get all these levels.&lt;/P&gt;
&lt;P&gt;2)CALL EXECUTE() to make it happen .&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 12:25:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-an-indicator-variable-based-on-whether-a-row-exists/m-p/451057#M113655</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-04-04T12:25:37Z</dc:date>
    </item>
  </channel>
</rss>

