## histogram

Occasional Contributor
Posts: 6

# histogram

Hi,please how to construct a histogram for a set of means say X1-X1000 that Ihave calculated?

I did the following:

proc univariate data=pop;

var mn_X1-mn_X1000;

histogram;

run;

where mn_x means the mean of x1

but they gave me many histograms fo each mean but I need only one histogram for all the means.

thanks a lot

Super User
Posts: 23,663

## Re: histogram

Transpose your data so that you have a two variables

VariableNum Mean

1   25

2   34

3   45

4  34

...

1000 45

And then run proc univariate on the mean variable.

You can look into proc transpose to flip the data.

Posts: 5,521

## Re: histogram

An implementation of 's suggestion would be :

data popList;

length id \$12;

set pop;

array nm{*} nm_x:;

do i = 1 to dim(nm);

x = nm{i};

id = catx("_", _n_, i);

output;

end;

keep id x;

run;

proc univariate data=popList;

var x;

id id;

histogram;

run;

PG

PG
Occasional Contributor
Posts: 6

## Re: histogram

sorry but it doesnt work

Posts: 5,521

how?

PG
SAS Employee
Posts: 19

## Re: histogram

PGStats used nm_x as a prefix for the variable names, rather than mn_x. If you change the code to use mn_x as a prefix, the code works perfectly. The code below demonstrates that with three means.

data pop;

input mn_x1 mn_x2 mn_x3;

datalines;

1 5 7

;

run;

data popList;

length id \$12;

set pop;

array mn{*} mn_x:;

do i = 1 to dim(mn);

x = mn{i};

id = catx("_", _n_, i);

output;

end;

keep id x;

run;

proc univariate data=popList;

var x;

id id;

histogram;

run;

Occasional Contributor
Posts: 6

## Re: histogram

I put the means in  a table as follows

data histo;

set proc_means;

array x{*}nm_x1-nm_x1000;

run;

now Iwant to put that table in a variable say F but I didnt find  a solution  I did the following but it doesnt work

data histo;

set proc_means;

array x{*}nm_x1-nm_x1000;

s=x{nm_x1-nm_x1000};

run;

Occasional Contributor
Posts: 6

## Re: histogram

I put the means in  a table as follows

data histo;

set proc_means;

array x{*}nm_x1-nm_x1000;

run;

now Iwant to put that table in a variable say S but I didnt find  a solution  I did the following but it doesnt work

data histo;

set proc_means;

array x{*}nm_x1-nm_x1000;

s=x{nm_x1-nm_x1000};

run;

SAS Employee
Posts: 19

## Re: histogram

The easiest way to do that is to use the TRANSPOSE procedure. After you transpose the data, you'll need to drop the _name_ variable and rename the col1 variable to s. The code below shows how to do this.

data proc_means;

input mn_x1 mn_x2 mn_x3;

datalines;

1 5 7

;

run;

proc transpose data=proc_means out=histo;

var mn:;

run;

data histo;

set histo (drop=_NAME_ rename=col1=s);

run;

Super User
Posts: 23,663

## Re: histogram

Your code is missing the do loop portion from PG's code.

If you open the data set does it look like the structure I've recommended in my post?

SAS Employee
Posts: 19

## Re: histogram

The do loop is not necessary after using the TRANSPOSE procedure. The data set would appear as follows:

S

1

5

7

If you want to keep the variable label for each observation, just remove "drop=_name_" from the code I previously posted and the result is as follows.

The _name_ variable is not necessary for generating the histogram, but you can keep it if you like. The code for generating the histogram is:

proc univariate data=histo;

var s;

histogram;

run;

Super User
Posts: 23,663

## Re: histogram

I was trying to explain why the code used did not work, it was not a full implementation of the suggested code.

The code suggested by PG should work as well, just a different way to transpose.

SAS Employee
Posts: 19

## Re: histogram

I misunderstood. You are correct that the code suggested by PG would also work, as long as the variable names are changed to be the same as those used by mesdz (e.g., mn_x1, not nm_x1).

Occasional Contributor
Posts: 6

## Re: histogram

you meant that I have to replace nm_x1 by mn_x1?

SAS Employee
Posts: 19

## Re: histogram

Yes, exactly. Or you can just copy the code that I posted previously.

Discussion stats
• 16 replies
• 647 views
• 0 likes
• 4 in conversation