<?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: Create flag based on multiple observations for each subject in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-flag-based-on-multiple-observations-for-each-subject/m-p/354107#M82779</link>
    <description>&lt;P&gt;I would suggest that you are better off if:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Your "flags" should be numeric, not character, and&lt;/LI&gt;
&lt;LI&gt;they should be counts, not 0/1 flags&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Getting that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;if first.id then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; missing_count=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; invalid_count=0;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;if category=' ' then missing_count + 1;&lt;/P&gt;
&lt;P&gt;else if category not in ('AAA', 'BBB', 'CCC') then invalid_count + 1;&lt;/P&gt;
&lt;P&gt;if last.id;&lt;/P&gt;
&lt;P&gt;keep id missing_count invalid_count;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Apr 2017 13:30:51 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-04-27T13:30:51Z</dc:date>
    <item>
      <title>Create flag based on multiple observations for each subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-flag-based-on-multiple-observations-for-each-subject/m-p/354101#M82776</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For your kind information, I am trying to create a flag based on multiple observations for each subject. For example, multiple observations for a subject, if any observation value is missing, one missing flag will be created as well as if any of the value is invalid, an invalid flag will be created. In my example given bleow, valid range of values for category are AAA, BBB and CCC. So, if category contains DDD must be flagged as invalid.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length id $3. category $3. ;
infile datalines TRUNCOVER;
input id $ category $;
datalines;
101 AAA
101 BBB
101 CCC
201 AAA
201    
201 BBB
301 AAA
301    
301 DDD
;
run;

data want;
length id $3. missing_flag $1. invalid_flag $1;
infile datalines TRUNCOVER;
input id $  missing_flag $ invalid_flag $ ;
datalines;
101 0 0 
201 1 0
301 1 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you in advance for your kind guidance.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 13:18:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-flag-based-on-multiple-observations-for-each-subject/m-p/354101#M82776</guid>
      <dc:creator>DeepakSwain</dc:creator>
      <dc:date>2017-04-27T13:18:41Z</dc:date>
    </item>
    <item>
      <title>Re: Create flag based on multiple observations for each subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-flag-based-on-multiple-observations-for-each-subject/m-p/354104#M82777</link>
      <description>&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;data have;
  length id $3. category $3. ;
  infile datalines TRUNCOVER;
  input id $ category $;
datalines;
101 AAA
101 BBB
101 CCC
201 AAA
201    
201 BBB
301 AAA
301    
301 DDD
;
run;
data want (keep=id missing_flag invalid_flag);
  set have;
  by id;
  retain missing_flag invalid_flag;
  if first.id then do;
    missing_flag=0;
    invalid_flag=1;
  end;
  if category="" then missing_flag=1;
  if category not in ("AAA","BBB","CCC") then invalid_flag=0;
  if last.id then output;
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Apr 2017 13:26:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-flag-based-on-multiple-observations-for-each-subject/m-p/354104#M82777</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-27T13:26:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create flag based on multiple observations for each subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-flag-based-on-multiple-observations-for-each-subject/m-p/354107#M82779</link>
      <description>&lt;P&gt;I would suggest that you are better off if:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Your "flags" should be numeric, not character, and&lt;/LI&gt;
&lt;LI&gt;they should be counts, not 0/1 flags&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Getting that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;if first.id then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; missing_count=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; invalid_count=0;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;if category=' ' then missing_count + 1;&lt;/P&gt;
&lt;P&gt;else if category not in ('AAA', 'BBB', 'CCC') then invalid_count + 1;&lt;/P&gt;
&lt;P&gt;if last.id;&lt;/P&gt;
&lt;P&gt;keep id missing_count invalid_count;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 13:30:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-flag-based-on-multiple-observations-for-each-subject/m-p/354107#M82779</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-27T13:30:51Z</dc:date>
    </item>
    <item>
      <title>Re: Create flag based on multiple observations for each subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-flag-based-on-multiple-observations-for-each-subject/m-p/354112#M82781</link>
      <description>&lt;P&gt;An alternative solution, using sql code (which may be used in many statistical and database applications such as R, SPSS, MSSQL, etc.):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length id $3. category $3. ;
infile datalines TRUNCOVER;
input id $ category $;
datalines;
101 AAA
101 BBB
101 CCC
201 AAA
201    
201 BBB
301 AAA
301    
301 DDD
;
run;

proc sql;
create table flags as
select id
, max(case when category = "" then 1 else 0 end) as missing_flag
, max(case when category in ('DDD') then 1 else 0 end) as invalid_flag
from have
group by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Apr 2017 13:48:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-flag-based-on-multiple-observations-for-each-subject/m-p/354112#M82781</guid>
      <dc:creator>thomp7050</dc:creator>
      <dc:date>2017-04-27T13:48:42Z</dc:date>
    </item>
    <item>
      <title>Re: Create flag based on multiple observations for each subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-flag-based-on-multiple-observations-for-each-subject/m-p/354115#M82784</link>
      <description>&lt;P&gt;Hi Astounding,&lt;/P&gt;&lt;P&gt;Thank you for providing me a proactive solution by including counter.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 13:54:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-flag-based-on-multiple-observations-for-each-subject/m-p/354115#M82784</guid>
      <dc:creator>DeepakSwain</dc:creator>
      <dc:date>2017-04-27T13:54:16Z</dc:date>
    </item>
  </channel>
</rss>

