<?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: COunt by group in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627561#M20539</link>
    <description>&lt;P&gt;So I do not need a count of how many times a 'Yes' occurs, I need a variable with a 'Yes' indicator so that I can run a cross tabs of that and a grouping variable. Thank you&lt;/P&gt;</description>
    <pubDate>Wed, 26 Feb 2020 16:26:11 GMT</pubDate>
    <dc:creator>GS2</dc:creator>
    <dc:date>2020-02-26T16:26:11Z</dc:date>
    <item>
      <title>COunt by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627555#M20537</link>
      <description>&lt;P&gt;Using SAS 9.4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a variable on_opioids (yes/no) and I need to know if a person was ever a 'Yes'. The problem is I need to know within a record_ID variable and also within a time_point variable (3 categorical timepoints)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;record_id&lt;/TD&gt;&lt;TD&gt;timepoint&lt;/TD&gt;&lt;TD&gt;opioid&lt;/TD&gt;&lt;TD&gt;WANT&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;pre&lt;/TD&gt;&lt;TD&gt;yes&lt;/TD&gt;&lt;TD&gt;yes&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;first&lt;/TD&gt;&lt;TD&gt;no&lt;/TD&gt;&lt;TD&gt;no&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;second&lt;/TD&gt;&lt;TD&gt;yes&lt;/TD&gt;&lt;TD&gt;yes&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;pre&lt;/TD&gt;&lt;TD&gt;no&lt;/TD&gt;&lt;TD&gt;yes&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like something like above where if the record_id was ever on an opioid within the timepoint it reports a "yes". Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 16:05:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627555#M20537</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2020-02-26T16:05:48Z</dc:date>
    </item>
    <item>
      <title>Re: COunt by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627556#M20538</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
    tables record_id*timepoint*opioid/noprint out=_counts_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In the output data set _COUNTS_, if you have OPIOID='Yes' and the count is 1 or more, then this is what you are looking for.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 16:16:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627556#M20538</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-26T16:16:20Z</dc:date>
    </item>
    <item>
      <title>Re: COunt by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627561#M20539</link>
      <description>&lt;P&gt;So I do not need a count of how many times a 'Yes' occurs, I need a variable with a 'Yes' indicator so that I can run a cross tabs of that and a grouping variable. Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 16:26:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627561#M20539</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2020-02-26T16:26:11Z</dc:date>
    </item>
    <item>
      <title>Re: COunt by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627568#M20540</link>
      <description>&lt;P&gt;If the COUNT of 'Yes' is 1 or more, you get the "Yes" indicator you are asking for. It sounds like you want to merge the output data set back into the original data set so that you can create this "Yes" indicator.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 16:35:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627568#M20540</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-26T16:35:15Z</dc:date>
    </item>
    <item>
      <title>Re: COunt by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627573#M20542</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/194348"&gt;@GS2&lt;/a&gt;&amp;nbsp; Do you mean this by any chance?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input record_id $	timepoint $	opioid $;*	WANT;
cards;
2	pre	yes	yes
2	first	no	no
2	second	yes	yes
2	pre	no	yes
;

data want;
 do _n_=1 by 1 until(last.record_id);
  set have;
  by record_id;
  if timepoint^='first' and opioid='yes' then t=1;
 end;
 do _n_=1 to _n_;
  set have;
  length want $3;
  want='no';
  if t then if timepoint^='first' then want='yes';
  output;
 end;
 drop t;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Feb 2020 16:41:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627573#M20542</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-26T16:41:28Z</dc:date>
    </item>
    <item>
      <title>Re: COunt by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627582#M20546</link>
      <description>&lt;P&gt;When I ran this code the new variable came out as "no" for all observations&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 17:14:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627582#M20546</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2020-02-26T17:14:56Z</dc:date>
    </item>
    <item>
      <title>Re: COunt by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627586#M20547</link>
      <description>&lt;P&gt;The logic I assumed is this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Identify is there is Opioid is 'Yes' for a record where timepoint is not first. If this is identified successfully flag it.&lt;/P&gt;
&lt;P&gt;2. For the same group record_id ,should the flag t=1 , assign want=yes for the records&amp;nbsp;timepoint not ='first' and No for this record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I trust you are able to understand the above logic. If this approach is correct, you would have to verify CASE SENSITIVITY of your values.&amp;nbsp; What is also important is your sample is a representative of your real.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 17:24:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627586#M20547</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-26T17:24:22Z</dc:date>
    </item>
    <item>
      <title>Re: COunt by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627590#M20548</link>
      <description>&lt;P&gt;I understand the logic and double checked the case sensitivity but still came up all no&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 17:37:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627590#M20548</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2020-02-26T17:37:18Z</dc:date>
    </item>
    <item>
      <title>Re: COunt by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627594#M20549</link>
      <description>&lt;P&gt;PROC FREQ works, doesn't matter what the capitalization is. Then you merge the PROC FREQ results back into the original data set.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 17:41:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627594#M20549</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-26T17:41:08Z</dc:date>
    </item>
    <item>
      <title>Re: COunt by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627596#M20551</link>
      <description>&lt;P&gt;hmm that's strange as it works for the sample &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 17:51:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627596#M20551</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-26T17:51:28Z</dc:date>
    </item>
    <item>
      <title>Re: COunt by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627601#M20553</link>
      <description>&lt;P&gt;My fault I added a space when changing the capitalization of an answer option. Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 18:05:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627601#M20553</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2020-02-26T18:05:01Z</dc:date>
    </item>
    <item>
      <title>Re: COunt by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627602#M20554</link>
      <description>&lt;P&gt;It is not clear from you example what you want.&amp;nbsp; Also the variables in your example do not match the names in your description.&lt;/P&gt;
&lt;P&gt;It sounds like you want two flags created, but your sample data only has one WANT variable.&lt;/P&gt;
&lt;P&gt;Try this and see if either of these is what you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input record_id timepoint $ opioid $ WANT $;
cards;
2 pre yes yes
2 first no no
2 second yes yes
2 pre no yes
;

proc sql ;
create table want as 
select a.*
     , b.any_time
     , c.this_time
from have a 
inner join (select record_id,max(opioid) as any_time from have group by record_id) b
on a.record_id = b.record_id 
inner join (select record_id,timepoint,max(opioid) as this_time from have group by record_id,timepoint) c
on a.record_id = c.record_id and a.timepoint=c.timepoint
order by record_id,timepoint,opioid
;
quit;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;       record_                                               this_
Obs       id      timepoint    opioid    WANT    any_time    time

 1        2        first        no       no        yes        no
 2        2        pre          no       yes       yes        yes
 3        2        pre          yes      yes       yes        yes
 4        2        second       yes      yes       yes        yes&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Feb 2020 18:05:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/COunt-by-group/m-p/627602#M20554</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-26T18:05:27Z</dc:date>
    </item>
  </channel>
</rss>

