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
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;
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;
: 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.
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;
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
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.