BookmarkSubscribeRSS Feed
avikam
Calcite | Level 5

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:

 

h1mx1h2mx2
10125.217525
1752422022
22024.710120
1012225515.1
3012517525.2
1221510116
    
17 REPLIES 17
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

avikam
Calcite | Level 5

i want collect the "101" from h1 column and column h2 

data_null__
Jade | Level 19

@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;
Astounding
PROC Star

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

Shmuel
Garnet | Level 18

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;
Astounding
PROC Star

A couple of issues to consider ...

 

  • How do you limit the results to the range of 101 to 175?
  • How do you get the right result when h1=h2?

It's all fixable, but those details could matter.

Shmuel
Garnet | Level 18

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;
avikam
Calcite | Level 5

In my data h1 ne h2.

Shmuel
Garnet | Level 18

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.

avikam
Calcite | Level 5
For h1=101 or h2=101

(25.2+20+22+16)/4



And so son for h1=175 or h2=175


Shmuel
Garnet | Level 18

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.

Astounding
PROC Star

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.

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!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 17 replies
  • 1609 views
  • 1 like
  • 7 in conversation