turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Studio
- /
- array?

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-16-2017 05:14 AM

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 |

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-16-2017 05:24 AM

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

Maxims of Maximally Efficient SAS Programmers

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-16-2017 05:51 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-16-2017 05:59 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-16-2017 06:45 AM

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-16-2017 07:13 AM

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

Maxims of Maximally Efficient SAS Programmers

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-16-2017 08:05 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-16-2017 10:00 AM

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-16-2017 10:08 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-16-2017 10:15 AM

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-19-2017 03:06 AM

In my data h1 ne h2.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-19-2017 06:39 AM

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**.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-19-2017 09:02 AM

For h1=101 or h2=101

(25.2+20+22+16)/4

And so son for h1=175 or h2=175

(25.2+20+22+16)/4

And so son for h1=175 or h2=175

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-19-2017 09:13 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-19-2017 12:35 PM

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.