BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
msrenee1984
Obsidian | Level 7

Hello, I'm inquiring how to SUM a column of student hours (1:30, 1:45, etc.) by their student ID, class subject, and YEAR.  Would like my report to show total class hours by each student for each year for year subject("sType").  I've attached an example...actual dataset has thousands of rows.  Thank you for any assistance!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data new;
    set old;
    hours_n=input(hours,hhmmss5.);
    format hours_n hhmm5.;
run;

 

Data set NEW should be run through PROC SUMMARY using the new variable named HOURS_N, and PROC SUMMARY should do the summing properly.

--
Paige Miller

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

Any method in SAS that sums a numeric variable will work. 

 

Almost no one here will download Excel files as they can be security threats. The best way to include data in your example is via working SAS data step  code (instructions and examples).

 

For example, here is code using PROC SUMMARY which does the summing. I have made up variable names since I don't know the variable names in your Excel file. This assumes that 1:30 (one hour 30 minutes) is numeric and unformatted appears as 5400 (which is 60 seconds per minute * 60 minutes per hour * 1.5 hours). It gives the sum of the number of seconds. It also works if 1 hour and 30 minutes is represented as 1.5, then it gives the sum of the number of hours.

 

proc summary data = ____________;
    class stype year student_id;
    var hours;
    output out=sums sum=sum_hours;
run;

 

--
Paige Miller
msrenee1984
Obsidian | Level 7

Thank you very much Paige... I will reframe from uploading excel and just post my code in my question!!! I will try the PROC summary now!! (Yes, its 1hr and 30 minutes!)

msrenee1984
Obsidian | Level 7

I'm getting an error message that says "ERROR: Variable HOURS in list does not match type prescribed for this list.  I'm unsure how to convert the "hours" variable...

PaigeMiller
Diamond | Level 26

Your hours must be character variables, not numeric variables. You can't sum character variables. You can convert these into actual numbers. If you can show us how the variable hours appears in your data (just type in an example), we can advise further.

--
Paige Miller
msrenee1984
Obsidian | Level 7

Yes, I just double checked it's a character variable CHAR length 5.  Attaching the actual column below.  Thank you!

HOURS
1:30
1:45
1:30
1:45
0:00
1:30
1:45
1:30
1:45
1:30
1:45
1:30
1:45
0:00
1:30
1:45
0:00
1:00
1:30
1:45
2:00
1:30
1:45
1:30
1:45
1:30
1:45
1:30
1:45
0:00

 

PaigeMiller
Diamond | Level 26
data new;
    set old;
    hours_n=input(hours,hhmmss5.);
    format hours_n hhmm5.;
run;

 

Data set NEW should be run through PROC SUMMARY using the new variable named HOURS_N, and PROC SUMMARY should do the summing properly.

--
Paige Miller
msrenee1984
Obsidian | Level 7

this worked perfectly, thank you very much!!!

Kurt_Bremser
Super User

@msrenee1984 wrote:

I'm getting an error message that says "ERROR: Variable HOURS in list does not match type prescribed for this list.  I'm unsure how to convert the "hours" variable...


This means that your hours variable is most probably of type character.

Follow the instructions posted by @PaigeMiller and post the resulting code. This is mandatory!

Only then will we know important variable attributes (type, length, format) and the real content we have to work with.

Excel spreadsheets do not have this information, only what Excel thinks about it.

 

Also answer questions like where does the data originate from, and how do you read it into your SAS environment?

Kurt_Bremser
Super User

Also note that the communities website does not provide the usual online preview which would allow us to view the spreadsheet without downloading. This is suspicious.

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
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
  • 9 replies
  • 1429 views
  • 0 likes
  • 3 in conversation