## Doing Average

Solved
Super Contributor
Posts: 1,041

# Doing Average

Hi Team,

Desc            stay

Hosp            32

Hosp            20

Hosp            15

apartment       5

vacation       20

vacation       12

vacation       5

vacation      10

If is the first of the case i want to create stay1 variable and say stay1=stay

elso do stay1=the avarage of stay

Output is like

Desc            stay1

Hosp            22.33

apartment       5

vacation         11.75

Accepted Solutions
Solution
‎01-12-2013 03:15 PM
Super User
Posts: 8,073

## Re: Doing Average

Normally to summarize you would use PROC SUMMARY.

proc summary data=have nway missing;

class desc ;

var stay1 ;

output out=want mean=stay1;

run;

You could do it with SQL

proc sql ;

create table want as

select desc,mean(stay) as stay1

from have

group by 1

;

quit;

You could even do it with a data step, but why would you?

data want ;

do _n_=1 by 1 until (last.desc);

set have;

by desc;

stay1 = sum(stay,stay1);

end;

stay1=stay1/_n_;

run;

All Replies
Solution
‎01-12-2013 03:15 PM
Super User
Posts: 8,073

## Re: Doing Average

Normally to summarize you would use PROC SUMMARY.

proc summary data=have nway missing;

class desc ;

var stay1 ;

output out=want mean=stay1;

run;

You could do it with SQL

proc sql ;

create table want as

select desc,mean(stay) as stay1

from have

group by 1

;

quit;

You could even do it with a data step, but why would you?

data want ;

do _n_=1 by 1 until (last.desc);

set have;

by desc;

stay1 = sum(stay,stay1);

end;

stay1=stay1/_n_;

run;

PROC Star
Posts: 8,163

## Re: Doing Average

: The definition of a mean is the sum of values divided by the number of values thus, in this case, the sum of all 10 values divided by 10.  Since there will likely be more codes that you have to group together, it will probably be easiest to create a format that reflects the necessary groupings and incorporate the format into your analysis.

Contributor
Posts: 22

## Re: Doing Average

As Tom suggested you can do it using "Proc Summary". You can also do using "Proc Means":

```proc means data=test mean;
class desc;
var stay;
OUTPUT OUT=avg;
run;
```
Super Contributor
Posts: 1,041

## Re: Doing Average

Hi,

In the SQL method was does the :

GROUP BY 1 mean????

I GET THE ANSWER BUT WOULD IT NOT BE "GROUP BY DESC?????

ALSO what does:

desc,mean(stay) mean????

Regards

PROC Star
Posts: 8,163

## Re: Doing Average

First question: You can identify variables by the position number (left to right) from the order they are listed on the select statement.  Thus, since desc was listed first, it can be specified as 1.  That is sometimes important because a new variable might be computed in the select statement, but not explicitly assigned a name.

Second question: desc and stay are the names of your two variables.  Thus it is selecting desc and the mean of stay.  Tom's suggested code actually specified select desc,mean(stay) mean as stay1, which was saying select desc and the mean of stay, and assign the mean to a variable called stay1.

🔒 This topic is solved and locked.