BookmarkSubscribeRSS Feed
eer_seer
Obsidian | Level 7

So, let's say I have a list of student enrollments by class. 

 

Each observation lists the student id (ID), their credits attempted (SCH), and their class (class_id).  Through proc tabulate, how do I tabulate the total number of hours (instead of counts) for each course for all students?  I've already tried by changing the VAR statement to var SCH; but that still delivers counts. Is there a way to change that to deliver the SUM of the SCH instead of the count...

7 REPLIES 7
Reeza
Super User
varName*sum*f=12.

You specify that you want the sum by including the variable and the word sum separated by an asterisk.

You can have multiple variables and statistics in a PROC TABULATE so you can have multiple vars in your VAR statement, but it should be the one you want to count/display. You could also use it as a weight/freq variable but you can get unexpected behavior if you don't fully understand how that works.

eer_seer
Obsidian | Level 7
Ok, great. Now, where do I apply this? I keep getting errors for the var statement?
ballardw
Super User

@eer_seer wrote:
Ok, great. Now, where do I apply this? I keep getting errors for the var statement?

When you get an error best practice on the forum is to copy the data step or procedure code along with all messages, notes, warnings and errors from the log, and paste the copied text into a code box opened on the forum with the </> icon. The code box is important to preserve the formatting of the text and any diagnostics that SAS provides in the log. The main message windows on this forum will reformat pasted text making the information less useful.

 

VAR variables in Proc Tabulate must be numeric.

eer_seer
Obsidian | Level 7
27         proc tabulate data=ir.class_enrollment_improved missing;
28         title1 'Enrollment In Specific Courses for ALL Enrolled Engineering Students - Fall 2020';
29         class strm sch class_subject_cd class_subject_ldesc class_acad_org_improved CLASS_ACAD_CAREER ;
30         var enrl_units_billing*sum*f=12.;
                                 _
                                 22
                                 200
ERROR 22-322: Syntax error, expecting one of the following: a name, ;, -, /, :, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.  
ERROR 200-322: The symbol is not recognized and will be ignored.
31         table class_acad_org_improved = 'Department Offering Class'*class_subject_cd = 'Class Subj. Code'*class_subject_ldesc =
31       ! 'Subject' all, strm; run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE TABULATE used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

 

mkeintz
PROC Star

While the sum operator is to be applied to the analysis variable, it is not meant to be in the VAR statement listing the analysis variables.  It should be in the TABLE statement, as in:

 

proc tabulate data=ir.class_enrollment_improved missing;
  title1 'Enrollment In Specific Courses for ALL Enrolled Engineering Students - Fall 2020';
  class strm sch class_subject_cd class_subject_ldesc class_acad_org_improved CLASS_ACAD_CAREER ;

  var enrl_units_billing;

  table class_acad_org_improved = 'Department Offering Class'
      * class_subject_cd        = 'Class Subj. Code'
      * class_subject_ldesc     =' Subject' 
      all
    , strm *var enrl_units_billing*sum*f=12.;

run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

@eer_seer wrote:
27         proc tabulate data=ir.class_enrollment_improved missing;
28         title1 'Enrollment In Specific Courses for ALL Enrolled Engineering Students - Fall 2020';
29         class strm sch class_subject_cd class_subject_ldesc class_acad_org_improved CLASS_ACAD_CAREER ;
30         var enrl_units_billing*sum*f=12.;
                                 _
                                 22
                                 200
ERROR 22-322: Syntax error, expecting one of the following: a name, ;, -, /, :, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.  
ERROR 200-322: The symbol is not recognized and will be ignored.
31         table class_acad_org_improved = 'Department Offering Class'*class_subject_cd = 'Class Subj. Code'*class_subject_ldesc =
31       ! 'Subject' all, strm; run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE TABULATE used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

 


Var statement in Proc Tabulate is intended to 1) list numeric variables and 2) apply some options, basically either appearance options such as text appearance or the weight option to specify a weight. No statistics (body of a Table statement), no formats (body of Table statement), no Labels (Label statement or body of Table).

Reeza
Super User

When in doubt check the documentation. 

The third example has an example of what you're trying to do.

 

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=proc&docsetTarget=n128ae2...

 

proc tabulate data=energy format=comma12.;
   class region division type;
   var expenditures;
   table region*(division all='Subtotal')
      all='Total for All Regions'*f=dollar12.,
      type='Customer Base'*expenditures=' '*sum=' '
         all='All Customers'*expenditures=' '*sum=' '
      / rts=25;
   format region regfmt. division divfmt. type usetype.;
   title 'Energy Expenditures for Each Region';
   title2 '(millions of dollars)';
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1772 views
  • 1 like
  • 4 in conversation