<?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: Summarize repeated rows of categorical data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Summarize-repeated-rows-of-categorical-data/m-p/728041#M226538</link>
    <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face="inherit"&gt;I tried to create a new variable using data step with BY statement, used first.subject last.subjectID tutorials, but nothing has worked.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="inherit"&gt;What doesn't work? Explain this to us. What is the evidence that something isn't working properly? Show us the evidence.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="inherit"&gt;It might also help if you provide a portion of the data set named HAVE, following &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;these instructions&lt;/A&gt; (and not via any other method).&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 21 Mar 2021 22:49:44 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-03-21T22:49:44Z</dc:date>
    <item>
      <title>Summarize repeated rows of categorical data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarize-repeated-rows-of-categorical-data/m-p/728039#M226536</link>
      <description>&lt;P&gt;Hello all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a complete newbie to SAS and mostly survived through this forum and UCLA thus far. I have rows of data on same subject (1 to 20+ occurrences). I have combined them using PROC means&amp;nbsp;&lt;FONT face="inherit"&gt;by class SubjectID, which worked well for &lt;/FONT&gt;continuous&lt;FONT face="inherit"&gt;&amp;nbsp;variables. &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="inherit"&gt;However, I have a few categorical variables (e.g. WhichHand?, WhatTime?) for which I need one conditional observation per subject. For example, if a subject used only R hand=1 then newvar=1, only L=2 then newvar=2, but if they used R and L=2 OR both=3&amp;nbsp; then newvar=3.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="inherit"&gt;I tried to create a new variable using data step with BY statement, used first.subject last.subjectID tutorials, but nothing has worked.&amp;nbsp;Instead of new variable doing a loop of all observation for subject, it basically copies the old variable.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="inherit"&gt;I am posting my most recent attempt, but I've basically given up and may just end up using mode or max in proc summary/means with By SubjectID at this point.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;PROC FORMAT;
	VALUE TIMED
	0 = 'morning' 
	1 = 'afternoon'
	2 = 'evening' 		
	3 = 'overnight';
	VALUE HAND
		1 = 'Left'
		2 = 'Right'
		3 = 'Both';
run;
DATA attemptWhichHand;
	SET have;
	BY subject_id;
	IF ((WhichHand=3)) THEN HandUsed = 3;
	else IF ((WhichHand=1) AND (WhichHand=2) AND (WhichHand=3)) THEN HandUsed = 3;	
	else IF ((WhichHand=1) AND (WhichHand=2)) THEN HandUsed = 3;
	else IF ((WhichHand=1) AND (WhichHand=3)) THEN HandUsed = 3;
	else IF ((WhichHand=2) AND (WhichHand=3)) THEN HandUsed = 3;
	else IF (WhichHand=1) THEN HandUsed = 1;
	else IF ((WhichHand=2)) THEN HandUsed = 2;  
	FORMAT HandUsed HAND.;
run;
/* This is what I did with other variables*/
proc means noprint data=HM_Meta.glasssheet NWAY ;
	class SUBJECT_ID ;
	id DOB;
	output out=hand (drop=_type_ rename=(_freq_=TotalCollect))
	Min (Age) = MinAgeWeek
	Max (Age) = MaxAgeWeek
	Range (Age) =  RAgeWeek
	Min (DATE) = MinDate
	Max (DATE) = MaxDate
	Max (Time WhichHand) = TimeMx WhichHandMx
/*Find a better way to summarize these variables - create new varibale only left, only right, both*/
run;/*925*/
proc print data=attempt (obs=50);    
run;&lt;/LI-CODE&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;PRE&gt;SubjectID date      WhichHand? Time   AgeMilk DOB .....
1      	24Jul2020       1         0
1       20Jul2020       2         2
1	29Jul2020 	1	  3
2	28Jul2020       3	  0 
3       30Jul2020&amp;nbsp;      1         4&lt;/PRE&gt;&lt;P&gt;Sorry about the poor quality of post and if this has been answered and thank you for your time &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Mar 2021 22:50:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarize-repeated-rows-of-categorical-data/m-p/728039#M226536</guid>
      <dc:creator>samraz</dc:creator>
      <dc:date>2021-03-21T22:50:56Z</dc:date>
    </item>
    <item>
      <title>Re: Summarize repeated rows of categorical data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarize-repeated-rows-of-categorical-data/m-p/728041#M226538</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face="inherit"&gt;I tried to create a new variable using data step with BY statement, used first.subject last.subjectID tutorials, but nothing has worked.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="inherit"&gt;What doesn't work? Explain this to us. What is the evidence that something isn't working properly? Show us the evidence.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="inherit"&gt;It might also help if you provide a portion of the data set named HAVE, following &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;these instructions&lt;/A&gt; (and not via any other method).&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Mar 2021 22:49:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarize-repeated-rows-of-categorical-data/m-p/728041#M226538</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-03-21T22:49:44Z</dc:date>
    </item>
    <item>
      <title>Re: Summarize repeated rows of categorical data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarize-repeated-rows-of-categorical-data/m-p/728185#M226577</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/374898"&gt;@samraz&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The issue with your code is that the IF conditions are checked for each record separately. But for a single observation a condition like&amp;nbsp;&lt;FONT face="courier new,courier"&gt;WhichHand=1 AND WhichHand=2&lt;/FONT&gt; is always FALSE because variable &lt;FONT face="courier new,courier"&gt;WhichHand&lt;/FONT&gt; cannot have different values at the same time. What you &lt;EM&gt;want&lt;/EM&gt; to check is whether&amp;nbsp;&lt;FONT face="courier new,courier"&gt;WhichHand=1&lt;/FONT&gt; for &lt;EM&gt;one&lt;/EM&gt; observation and &lt;FONT face="courier new,courier"&gt;WhichHand=2&lt;/FONT&gt; for &lt;EM&gt;another&lt;/EM&gt; observation of the same subject. So, it will help to &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=p0t2ac0tfzcgbjn112mu96hkgg9o.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;RETAIN&lt;/A&gt; the information across observations of a &lt;FONT face="courier new,courier"&gt;subject_id&lt;/FONT&gt; BY group and accumulate it until the last observation of the subject is reached.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One of several ways to achieve this is to use a temporary &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=p08do6szetrxe2n136ush727sbuo.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;array&lt;/A&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

data have;
input subject_id date :date9. WhichHand;
format date date9.;
cards;
1 24Jul2020 1
1 20Jul2020 2
1 29Jul2020 1
2 27Jul2020 2
2 28Jul2020 3
3 30Jul2020 2
3 31Jul2020 2
4 31Jul2020 1
;

/* Summarize WhichHand data */

data want(keep=subject_id HandUsed);
array h[3] _temporary_;
set have;
by subject_id;
if first.subject_id then call missing(of h[*]); /* initialize array for accumulated WhichHand data */
if WhichHand in (1:3) then h[WhichHand]=1;      /* tick "Left", "Right" or "Both" according to WhichHand */
if last.subject_id;                             /* continue once all obs. of a subject have been evaluated */
if h[3] or h[1] and h[2] then HandUsed=3;       /* collect information from the array to determine */
else if h[1] then HandUsed=1;                   /* the value of HandUsed                           */
else if h[2] then HandUsed=2;
else HandUsed=0;
format HandUsed HAND.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the three array elements &lt;FONT face="courier new,courier"&gt;h[1]&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;h[2]&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;h[3]&lt;/FONT&gt; are automatically retained and that their values, 1 or missing, are evaluated as TRUE or FALSE in IF statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Personally, I would feel tempted to use the (rarely used) &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n08a8qvo1xuzd1n189xdm0nay8h5.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;BOR function&lt;/A&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(keep=subject_id HandUsed);
set have;
by subject_id;
retain HandUsed;
if first.subject_id then HandUsed=0;
if WhichHand in (1:3) then HandUsed=bor(HandUsed, WhichHand);
if last.subject_id;
format HandUsed HAND.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But the array technique will generalize more easily to other categorical variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Mar 2021 13:53:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarize-repeated-rows-of-categorical-data/m-p/728185#M226577</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-22T13:53:08Z</dc:date>
    </item>
  </channel>
</rss>

