hi
h1 and h2 are index/
i want to group all h1=101 or h2=101 and calculate mx1 mx2 mean
so on to h1=175 or h2=175.
thanks
this is example of my data:
h1 | mx1 | h2 | mx2 |
101 | 25.2 | 175 | 25 |
175 | 24 | 220 | 22 |
220 | 24.7 | 101 | 20 |
101 | 22 | 255 | 15.1 |
301 | 25 | 175 | 25.2 |
122 | 15 | 101 | 16 |
Please post your example data in a data step, as described here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...
And post an example of your expected output.
POst test data in the form of a datastep, we are not here to either guess your data structure, nor act as data entry for you. As such this is just theory:
data want; set have; h_mean=mean(of h:); mx_mean=mean(of mx:); run;
Note the h: and mx: mean, all variable starting with h and mx respectively.
i want collect the "101" from h1 column and column h2
@avikam wrote:
i want collect the "101" from h1 column and column h2
This is what @RW9 means by "in the form of a data step". This can be copied and pasted into SAS program. Most folks don't want to download and EXCEL and fiddle with all that.
data h;
input h1 mx1 h2 mx2;
cards;
101 25.2 175 25
175 24 220 22
220 24.7 101 20
101 22 255 15.1
301 25 175 25.2
122 15 101 16
;;;;
run;
@avikam wrote:
i want collect the "101" from h1 column and column h2
That's not a SAS dataset. READ MY POST AGAIN.
Assuming you do create a SAS data set, and assuming that the variable names remain h1, h2, mx1, and mx2 ... the problem is difficult because the same observation could have h1=101 and h2=102. So that observation has to be included in more than one set of means This would be a way to process the data set 75 times to achieve the result you described:
%macro group_means;
%local i;
%do i=101 %to 175;
proc means data=have;
title "Statistics for &i";
where h1=&i or h2=&i;
var mx1 mx2;
run;
%end;
%mend group_means;
%group_means
Is that what you want:
Data tmp1;
set have;
h=h1; mx=mx1; output;
h=h2; mx=mx2; output;
keep h mx;
run;
proc means data=tmp1;
class h;
var mx;
output out=want mean=;
run;
A couple of issues to consider ...
It's all fixable, but those details could matter.
I assumed you want the mean of mx1 and mx2 as one variable mx.
The class=h eiminates to sort the data. When sorted you can use BY instead CLASS;
I you want to select H1=h2 (=h) in the range 101-175 you can filter data by where statement as in:
proc means data=tmp1(where=(h between 101 and 175));
class h;
var mx;
output out=want mean=;
run;
In my data h1 ne h2.
This is the data you posted:
h1 mx1 h2 mx2 101 25.2 175 25 175 24 220 22 220 24.7 101 20 101 22 255 15.1 301 25 175 25.2 122 15 101 16
And this was your request:
i want to group all h1=101 or h2=101 and calculate mx1 mx2 mean
Please explain, using above information, how would you calculate the mean manually.
Have tried run the code i have sent to you ?
Data tmp1; set have; h=h1; mx=mx1; output; h=h2; mx=mx2; output; keep h mx; run; /* use WHERE to select wanted range of h to calculate mean ) */ proc means data=tmp1(where=(h between 101 and 175)); class h; var mx; output out=want mean=; run;
Run the code and check results.
I think Schmuel has the right idea here. He follows what you want, where I was trying to compute separate means for mx1 and mx2.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.