Write and run SAS programs in your web browser

array?

Reply
Occasional Contributor
Posts: 6

array?

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
    
Esteemed Advisor
Posts: 5,984

Re: array?

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.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Esteemed Advisor
Esteemed Advisor
Posts: 6,726

Re: array?

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.

Occasional Contributor
Posts: 6

Re: array?

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

Respected Advisor
Posts: 3,768

Re: array?


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;
Esteemed Advisor
Posts: 5,984

Re: array?


avikam wrote:

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


That's not a SAS dataset. READ MY POST AGAIN.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 4,648

Re: array?

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

Super User
Posts: 1,079

Re: array?

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;
Respected Advisor
Posts: 4,648

Re: array?

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.

Super User
Posts: 1,079

Re: array?

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;
Occasional Contributor
Posts: 6

Re: array?

In my data h1 ne h2.

Super User
Posts: 1,079

Re: array?

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.

Occasional Contributor
Posts: 6

Re: array?

For h1=101 or h2=101

(25.2+20+22+16)/4



And so son for h1=175 or h2=175


Super User
Posts: 1,079

Re: array?

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.

Respected Advisor
Posts: 4,648

Re: array?

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.

Post a Question
Discussion Stats
  • 17 replies
  • 327 views
  • 1 like
  • 7 in conversation