BookmarkSubscribeRSS Feed
Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

hi guys,

if i want to find the total sum of all the by groups do i create another data set to find it or can i do it within this code below?

so far the code below computes the sum of score2 within each by group.But i also want to find the total sum of score2

Thanks

data a;

input status $ score1 score2;

cards;

medium 23 33

high 33 44

low 12 22

low 11 44

high 10 20

medium 33 66

medium . 44

;

proc sort;by status;

run;

data b;

do until(last.status);

set a;

by status;

sum=sum(sum,score2);

end;

proc print;run;

9 REPLIES 9
Reeza
Super User

proc means does it by default...

proc means data=a  sum;

ways 0 1;

class status;

output out=summary1 sum(score2)=sum;

run;

Ksharp
Super User

Why not add another variable ?

data b;

retain total ;

do until(last.status);

set a;

by status;

sum=sum(sum,score2);

total=sum(total,score2);

end;

Tom
Super User Tom
Super User

Do you want the total for all of the BY groups?

If you want to stick with the DOW theme then add another one that loops over the whole dataset once and retains the value onto the rest of the observations you are generating.

data b;

  if _n_=1 then do until (eof);

    set a end=eof;

    total+score2;

  end;

  do until(last.status);

    set a;

    by status;

    sum=sum(sum,score2);

  end;

run;

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

Tom,

if i only use the below part of your code :

data b;

  if _n_=1 then do until (eof);

    set a end=eof;

    total+score2;

  end;

proc print;run;


i'd get this output:


                           Obs    status    score1    score2    total

                            1     medium       .        44       273
                            2     medium       .        44       273


So my question is why do i get the observation twice,why not once only?

PGStats
Opal | Level 21

This has to do with the way SAS detects the end of a datastep execution. If you only want to scan the whole dataset, use this instead :

data b;

  do until (eof);

    set a end=eof;

    total + score2;

  end;

proc print;run;

PG
Tom
Super User Tom
Super User

This note in your log is clue as to what is happening:

NOTE: DATA STEP stopped due to looping.

SAS does not normally stop at the end of the data step. Normally it stops when you read past the end of file with in INPUT or SET/MERGE/UPDATE command. Since it can never read past the end of the file because of IF and DO conditions it runs through the data step twice before it notices on the second time that you did not input anything and stops the step.

PGStats
Opal | Level 21

Thank you Tom for explaining (again) that feature of the datastep. I can never remember the finer details of how this works... - PG

PG
Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

Thanks Tom and PGStats.Interesting thing.Never knew this capability if data-steps.

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

Thanks guys!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1045 views
  • 5 likes
  • 5 in conversation