<?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: Sum of Rows Joined with Other Table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618979#M181647</link>
    <description>&lt;P&gt;Also, here is a picture of the Data.grades table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Data.grades.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35532iC3B267DB50EB6824/image-size/large?v=v2&amp;amp;px=999" role="button" title="Data.grades.PNG" alt="Data.grades.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 21 Jan 2020 21:54:14 GMT</pubDate>
    <dc:creator>cneed</dc:creator>
    <dc:date>2020-01-21T21:54:14Z</dc:date>
    <item>
      <title>Sum of Rows Joined with Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618965#M181641</link>
      <description>&lt;P&gt;Hello, in question 3 I am trying to left join the data on student ID. In the Data.Grades table students can have more then 1 grade listed separately in several rows. When I join it to the Data.master_After2001 data set I am looking for the sum of the grades for each student. How can I accomplish this? My code right now is erroring on "(SUM(D.Grade)) AS Sum_of_Grades". I have provided all of my code for reference on how I got to question 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;/*Question 1:*/&lt;/STRONG&gt;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;CREATE TABLE Data.master_WithSchoolInfo AS&lt;BR /&gt;SELECT A.studentID, A.Birthdate, A.Gender, B.SchoolID, B.SchoolName&lt;BR /&gt;FROM Data.master A LEFT JOIN Data.school B&lt;BR /&gt;ON A.SchoolID = B.SchoolID;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;PROC CONTENTS DATA = Data.master_WithSchoolInfo;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;/*Question 2:*/&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;BR /&gt;CREATE TABLE Data.master_After2001 AS&lt;BR /&gt;SELECT *&lt;BR /&gt;FROM Data.master_WithSchoolInfo&lt;BR /&gt;WHERE Birthdate &amp;gt; '01Jan2001'd;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;PROC CONTENTS DATA = Data.master_After2001;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;/*Question 3:*/&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;BR /&gt;CREATE TABLE Data.master_After2001WithGrades AS&lt;BR /&gt;SELECT C.studentID, C.Birthdate, C.Gender, C.SchoolID, C.SchoolName, D.studentID, (SUM(D.Grade)) AS Sum_of_Grades&lt;BR /&gt;FROM Data.master_After2001 C Left Join Data.Grades D&lt;BR /&gt;ON C.studentID = D.studentID&lt;BR /&gt;GROUP BY D.Grade;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;PROC CONTENTS DATA = Data.master_After2001WithGrades;&lt;BR /&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2020 21:20:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618965#M181641</guid>
      <dc:creator>cneed</dc:creator>
      <dc:date>2020-01-21T21:20:15Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of Rows Joined with Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618971#M181643</link>
      <description>&lt;P&gt;It would be helpful if you showed us the LOG that gives the error (the code as seen in the log AND the errors as seen in the log).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It would be helpful if you showed us a portion of your data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a wild guess, if you have SUM(D.GRADE) and GROUP BY (D.GRADE), I'm thinking that might be a problem.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2020 21:27:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618971#M181643</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-21T21:27:07Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of Rows Joined with Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618973#M181644</link>
      <description>&lt;P&gt;Here Is what the error log is showing:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="sasSource"&gt;75 SELECT C.studentID, C.Birthdate, C.Gender, C.SchoolID, C.SchoolName, D.studentID, (SUM(D.Grade)) AS Sum_of_Grades&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;76 FROM Data.master_After2001 C Left Join Data.Grades D&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;77 ON C.studentID = D.studentID;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;ERROR: The SUM summary function requires a numeric argument.&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;I tried removing the group by but still no luck.&lt;/DIV&gt;</description>
      <pubDate>Tue, 21 Jan 2020 21:31:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618973#M181644</guid>
      <dc:creator>cneed</dc:creator>
      <dc:date>2020-01-21T21:31:13Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of Rows Joined with Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618979#M181647</link>
      <description>&lt;P&gt;Also, here is a picture of the Data.grades table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Data.grades.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35532iC3B267DB50EB6824/image-size/large?v=v2&amp;amp;px=999" role="button" title="Data.grades.PNG" alt="Data.grades.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2020 21:54:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618979#M181647</guid>
      <dc:creator>cneed</dc:creator>
      <dc:date>2020-01-21T21:54:14Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of Rows Joined with Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618983#M181649</link>
      <description>&lt;P&gt;Should be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE Data.master_After2001WithGrades AS
SELECT 
	C.studentID, 
	C.Birthdate, 
	C.Gender, 
	C.SchoolID, 
	C.SchoolName, 
	SUM(D.Grade) AS Sum_of_Grades
FROM 
	Data.master_After2001 as C Left Join 
	Data.Grades as D
ON C.studentID = D.studentID
GROUP BY C.studentID, C.Birthdate, C.Gender, C.SchoolID, C.SchoolName;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(unntested)&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2020 22:09:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618983#M181649</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-01-21T22:09:44Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of Rows Joined with Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618986#M181650</link>
      <description>&lt;P&gt;Still showing the error "the SUM summary function requires a numeric argument." I am trying to join the data.grades into the data.master_After2001 table, however, a student in the data file could have more then 1 grade. I am just looking for the amount of grades a student has to be entered into a new row in the Data.master_After2001 file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data.master_After2001:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="data.master_after2001.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35534iAE98C0F32F7FC556/image-size/large?v=v2&amp;amp;px=999" role="button" title="data.master_after2001.PNG" alt="data.master_after2001.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data.grades:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Data.grades.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35535i6F7CA1916CDF113A/image-size/large?v=v2&amp;amp;px=999" role="button" title="Data.grades.PNG" alt="Data.grades.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2020 22:14:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618986#M181650</guid>
      <dc:creator>cneed</dc:creator>
      <dc:date>2020-01-21T22:14:19Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of Rows Joined with Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618996#M181654</link>
      <description>&lt;P&gt;Aha!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are trying to sum letters A B C D F. Do you see why it doesn't work?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2020 22:42:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/618996#M181654</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-21T22:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of Rows Joined with Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/619003#M181657</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/308061"&gt;@cneed&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;As a simple remedy, you could just insert DISTINCT between &lt;SPAN&gt;SELECT and C.studentID, and you would not have the dupes. The query offered by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;results in the same (in his query, though, SUM should be replaced by COUNT, or else it won't work). However, neither is efficient. It makes more sense to aggregate the Grades file first, &amp;nbsp;which will collapse it to unique StudentID key-values, and then join the result with Master:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;                                                                                                                                       
  create table data.master_after2001withgrades as                                                                                                
  select m.studentid                                                                                                                             
       , m.birthdate                                                                                                                             
       , m.gender                                                                                                                                
       , m.schoolid                                                                                                                              
       , m.schoolname                                                                                                                            
       , g.grade_count                                                                                                                           
  from   data.master_after2001 c                                                                                                                 
    left join                                                                                                                                    
        (select studentid                                                                                                                        
              , count (grade) as grade_count                                                                                                     
         from   grades group studentid                                                                                                           
         ) g                                                                                                                                     
  on     m.studentid = g.studentid                                                                                                               
  ;                                                                                                                                              
quit ;       
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Alternatively, it can be achieved by applying the same concept by first aggregating Grades via a hash table:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop = grade) ;                                                                                                                       
  if _n_ = 1 then do ;                                                                                                                           
    dcl hash h () ;                                                                                                                              
    h.definekey ("studentid") ;                                                                                                                  
    h.definedata ("grade_count") ;                                                                                                               
    h.definedone () ;                                                                                                                            
    do until (z) ;                                                                                                                               
      set grades end = z ;                                                                                                                       
      if h.find() ne 0 then grade_count = 1 ;                                                                                                    
      else                  grade_count + 1 ;                                                                                                    
      h.replace() ;                                                                                                                              
    end ;                                                                                                                                        
  end ;                                                                                                                                          
  set master (keep = studentid birthdate gender schoolid schoolname) ;                                                                           
  if h.find() ne 0 then call missing (grade_count) ;                                                                                             
run ;            
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Neither SQL nor hash require the input data sets to be sorted by StudentID. However, if they are already sorted, the easiest and fastest way of getting what you want is a simple merge:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop = grade) ;                                                                                                                       
  do until (last.studentid) ;                                                                                                                    
    merge master (keep = studentid birthdate gender schoolid schoolname) grades (in = g) ;                                                       
    by studentid ;                                                                                                                               
    if g then grade_count = sum (grade_count, 1) ;                                                                                               
  end ;                                                                                                                                          
run ;  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;Of course, there're other ways of achieving the same result using more than a single step.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Kind regards&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Paul D.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2020 23:49:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/619003#M181657</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2020-01-21T23:49:36Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of Rows Joined with Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/619115#M181714</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;The query offered by&amp;nbsp;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462" target="_blank"&gt;@PGStats&lt;/A&gt;&amp;nbsp;results in the same (in his query, though, SUM should be replaced by COUNT, or else it won't work)&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I don't see how COUNT works any better than SUM, as it does not compute frequency of A and frequency of B, &lt;EM&gt;etc&lt;/EM&gt;. if that's what is being requested — clearly SUM makes no sense here. But &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/308061"&gt;@cneed&lt;/a&gt; really needs to state clearly the output he wants.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2020 11:54:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/619115#M181714</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-22T11:54:47Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of Rows Joined with Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/619171#M181730</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Grade is a character column here, hence SUM would generate:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#993366"&gt;ERROR: The SUM summary function requires a numeric argument.
&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;SUM (grade ne "") could be used but why when COUNT will do. On the other note, it does not follow from the OP's description that the count distinct is requested rather than the straight count. If so, it's easy to change, whether it's SQL, hash, or merge.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2020 14:44:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-of-Rows-Joined-with-Other-Table/m-p/619171#M181730</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2020-01-22T14:44:43Z</dc:date>
    </item>
  </channel>
</rss>

