<?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: frequency counts across multiple rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/frequency-counts-across-multiple-rows/m-p/575648#M162886</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/276208"&gt;@yoyong555&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;First, your input file DATA step cannot compile since you've missed $ after gender. Second, the variable AdmissionDate is irrelevant in the context of your question, so there's no need to show it. That having been said, if your input file is &lt;EM&gt;grouped&lt;/EM&gt; by (patientname, gender), as in your sample data, then it's a simple job for the DoW loop (note that below I've changed your input step accordingly):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data hosp ;                                                        
  input patientname :$20. gender :$1. proc1-proc3 length1-length3 ;
  cards ;                                                          
Maria F 1 1 0 8 2 .                                                
Maria F 0 1 1 . 5 3                                                
Jim   M 0 0 1 . . 7                                                
Luna  F 1 0 1 1 . 4                                                
run ;                                                              
                                                                   
data want (drop = proc: length:) ;                                 
  do until (last.gender) ;                                         
    set hosp ;                                                     
    by patientname gender notsorted ;                              
    n_procs = sum (n_procs, sum (of proc:)) ;                      
    array ls length: ;                                             
    do over ls ;                                                   
      if cmiss (ls) then continue ;                                
      if ls &amp;lt; 5 then len_lt5 = sum (len_lt5, 1) ;                  
      else           len_ge5 = sum (len_ge5, 1) ;                  
    end ;                                                          
    len_lt5 = sum (len_lt5, 0) ;                                   
    len_ge5 = sum (len_ge5, 0) ;                                   
  end ;                                                            
  label patientname = "Name of patient"                            
        n_procs = "No. of procedures"                              
        len_lt5 = "Length&amp;lt;5"                                       
        len_ge5 = "Length&amp;gt;=5"                                      
  ;                                                                
run ;                                                              
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If HOSP isn't really grouped by (patientname, gender), sort it by these keys beforehand.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If HOSP is neither grouped nor sorted and you don't want to sort it, the job still can be done by using the aggregation power of the hash object:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;                                                                
  if _n_ = 1 then do ;                                                       
    dcl hash h (ordered:"A") ;                                               
    h.definekey  ("patientname", "gender") ;                                 
    h.definedata ("patientname", "gender", "n_procs", "len_lt5", "len_ge5") ;
    h.definedone () ;                                                        
  end ;                                                                      
  set hosp end = z ;                                                         
  if h.find() ne 0 then do ;                                                 
    n_procs = 0 ;                                                            
    len_lt5 = 0 ;                                                            
    len_ge5 = 0 ;                                                            
  end ;                                                                      
  n_procs + sum (of proc:) ;                                                 
  array ls length: ;                                                         
  do over ls ;                                                               
    if cmiss (ls) then continue ;                                            
    if ls &amp;lt; 5 then len_lt5 + 1 ;                                             
    else           len_ge5 + 1 ;                                             
  end ;                                                                      
  h.replace() ;                                                              
  if z then h.output (dataset: "want") ;                                     
  label patientname = "Name of patient"                                      
        n_procs = "No. of procedures"                                        
        len_lt5 = "Length&amp;lt;5"                                                 
        len_ge5 = "Length&amp;gt;=5"                                                
  ;                                                                          
run ;                                                                        
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
    <pubDate>Tue, 23 Jul 2019 05:47:05 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-07-23T05:47:05Z</dc:date>
    <item>
      <title>frequency counts across multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/frequency-counts-across-multiple-rows/m-p/575635#M162877</link>
      <description>&lt;P&gt;Hi everyone.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this sample data.&lt;/P&gt;&lt;P&gt;proc = procedures (1 = did the procedure, 0 otherwise)&lt;/P&gt;&lt;P&gt;length = length of artery (if proc = 0 then length = blank)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data hosp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;input AdmissionDate date9. patientname $ 1-20 gender proc1 proc2 proc3 length1 length2 length3;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;datalines;&lt;/P&gt;&lt;P&gt;03May2011&amp;nbsp; Maria&amp;nbsp; F&amp;nbsp; 1 1 0&amp;nbsp; &amp;nbsp;8 2&amp;nbsp; .&lt;/P&gt;&lt;P&gt;10Jun2012&amp;nbsp; &amp;nbsp;Maria&amp;nbsp; F&amp;nbsp; 0 1 1&amp;nbsp; &amp;nbsp;.&amp;nbsp; 5 3&lt;/P&gt;&lt;P&gt;31Dec2015&amp;nbsp; Jim&amp;nbsp; &amp;nbsp; &amp;nbsp;M&amp;nbsp; 0 0 1&amp;nbsp; &amp;nbsp;.&amp;nbsp; .&amp;nbsp; 7&lt;/P&gt;&lt;P&gt;19Apr2002&amp;nbsp; &amp;nbsp;Luna&amp;nbsp; &amp;nbsp;F&amp;nbsp; 1 0 1&amp;nbsp; &amp;nbsp;1 .&amp;nbsp; 4&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i would like to have the following report.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Name of patient&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Gender&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;No. of procedures&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Length&amp;lt;5&amp;nbsp; &amp;nbsp;Length&amp;gt;=5&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;Maria&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jim&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;M&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;Luna&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks heaps!&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;Yoyong&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jul 2019 04:22:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/frequency-counts-across-multiple-rows/m-p/575635#M162877</guid>
      <dc:creator>yoyong555</dc:creator>
      <dc:date>2019-07-23T04:22:11Z</dc:date>
    </item>
    <item>
      <title>Re: frequency counts across multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/frequency-counts-across-multiple-rows/m-p/575648#M162886</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/276208"&gt;@yoyong555&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;First, your input file DATA step cannot compile since you've missed $ after gender. Second, the variable AdmissionDate is irrelevant in the context of your question, so there's no need to show it. That having been said, if your input file is &lt;EM&gt;grouped&lt;/EM&gt; by (patientname, gender), as in your sample data, then it's a simple job for the DoW loop (note that below I've changed your input step accordingly):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data hosp ;                                                        
  input patientname :$20. gender :$1. proc1-proc3 length1-length3 ;
  cards ;                                                          
Maria F 1 1 0 8 2 .                                                
Maria F 0 1 1 . 5 3                                                
Jim   M 0 0 1 . . 7                                                
Luna  F 1 0 1 1 . 4                                                
run ;                                                              
                                                                   
data want (drop = proc: length:) ;                                 
  do until (last.gender) ;                                         
    set hosp ;                                                     
    by patientname gender notsorted ;                              
    n_procs = sum (n_procs, sum (of proc:)) ;                      
    array ls length: ;                                             
    do over ls ;                                                   
      if cmiss (ls) then continue ;                                
      if ls &amp;lt; 5 then len_lt5 = sum (len_lt5, 1) ;                  
      else           len_ge5 = sum (len_ge5, 1) ;                  
    end ;                                                          
    len_lt5 = sum (len_lt5, 0) ;                                   
    len_ge5 = sum (len_ge5, 0) ;                                   
  end ;                                                            
  label patientname = "Name of patient"                            
        n_procs = "No. of procedures"                              
        len_lt5 = "Length&amp;lt;5"                                       
        len_ge5 = "Length&amp;gt;=5"                                      
  ;                                                                
run ;                                                              
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If HOSP isn't really grouped by (patientname, gender), sort it by these keys beforehand.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If HOSP is neither grouped nor sorted and you don't want to sort it, the job still can be done by using the aggregation power of the hash object:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;                                                                
  if _n_ = 1 then do ;                                                       
    dcl hash h (ordered:"A") ;                                               
    h.definekey  ("patientname", "gender") ;                                 
    h.definedata ("patientname", "gender", "n_procs", "len_lt5", "len_ge5") ;
    h.definedone () ;                                                        
  end ;                                                                      
  set hosp end = z ;                                                         
  if h.find() ne 0 then do ;                                                 
    n_procs = 0 ;                                                            
    len_lt5 = 0 ;                                                            
    len_ge5 = 0 ;                                                            
  end ;                                                                      
  n_procs + sum (of proc:) ;                                                 
  array ls length: ;                                                         
  do over ls ;                                                               
    if cmiss (ls) then continue ;                                            
    if ls &amp;lt; 5 then len_lt5 + 1 ;                                             
    else           len_ge5 + 1 ;                                             
  end ;                                                                      
  h.replace() ;                                                              
  if z then h.output (dataset: "want") ;                                     
  label patientname = "Name of patient"                                      
        n_procs = "No. of procedures"                                        
        len_lt5 = "Length&amp;lt;5"                                                 
        len_ge5 = "Length&amp;gt;=5"                                                
  ;                                                                          
run ;                                                                        
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jul 2019 05:47:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/frequency-counts-across-multiple-rows/m-p/575648#M162886</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-23T05:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: frequency counts across multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/frequency-counts-across-multiple-rows/m-p/575752#M162922</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data hosp ;                                                        
  input patientname :$20. gender :$1. proc1-proc3 length1-length3 ;
  cards ;                                                          
Maria F 1 1 0 8 2 .                                                
Maria F 0 1 1 . 5 3                                                
Jim   M 0 0 1 . . 7                                                
Luna  F 1 0 1 1 . 4  
; 
run ;

proc sql;
create table want as
 select patientname,gender,	
sum(proc1)+sum(proc2)+sum(proc3) as n_proc,
sum(.&amp;lt;length1&amp;lt;5)+sum(.&amp;lt;length2&amp;lt;5)+sum(.&amp;lt;length3&amp;lt;5) as  len_lt_5,
sum(length1&amp;gt;=5)+sum(length2&amp;gt;=5)+sum(length3&amp;gt;=5) as  len_ge_5
from hosp
   group by patientname,gender;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Jul 2019 11:49:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/frequency-counts-across-multiple-rows/m-p/575752#M162922</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-07-23T11:49:29Z</dc:date>
    </item>
    <item>
      <title>Re: frequency counts across multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/frequency-counts-across-multiple-rows/m-p/576441#M163171</link>
      <description>Thank you!</description>
      <pubDate>Thu, 25 Jul 2019 03:57:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/frequency-counts-across-multiple-rows/m-p/576441#M163171</guid>
      <dc:creator>yoyong555</dc:creator>
      <dc:date>2019-07-25T03:57:05Z</dc:date>
    </item>
  </channel>
</rss>

