Help using Base SAS procedures

Mean/Average Value for Dynamic Variables

Reply
Contributor
Posts: 55

Mean/Average Value for Dynamic Variables

Hi,

I want Average values of Mean or overall Mean value of dynamic variables. Variables may vary from project to project. Please some one help.

Thanks in Advance.


Data:
--------------------------------------------------
Project Country T1 T2
A INDIA 9 8
A US 7 6
A UK 5 4
---------------------------------------------------
Project Country T1 T2 T4
B INDIA 1 2 3
B US 5 4 7
B UK 6 8 9
---------------------------------------------------

Output Expected:
----------------------------------------------------------------------
Project Country T1 T2
A INDIA 9 8
A US 7 6
A UK 5 4
ALL_COUNTRY 7 6
----------------------------------------------------------------------

Project Country T1 T2 T4
B INDIA 1 2 3
B US 5 4 7
B UK 6 8 9
ALL_COUNTRY 4 4.67 6.33
----------------------------------------------------------------------
Super Contributor
Super Contributor
Posts: 365

Re: Mean/Average Value for Dynamic Variables

Hello 1239,

Look at proc TABULATE or proc REPORT for this purpose. A combination of proc MEANS with a datastep could help too.
"I want Average values of Mean or overall Mean value of dynamic variables" it is not very clear. Does it mean that input and output dataset names and variable names have to be parameters to some macro program calculating the overall mean?

Sincerely,
SPR
Contributor
Posts: 55

Re: Mean/Average Value for Dynamic Variables

Hi SPR,

Actually dynamic variable means for some project variables will be T1, T2 and for some projects will be T1, T2, T4 so on...

T1, T2, T4 these are created from Macro. So this is not constant and it may vary depending on the projects.

Currently i need average mean value for T1, T2, T4 so on in the last row as show in the output expected.

Please help.
SAS Super FREQ
Posts: 8,869

Re: Mean/Average Value for Dynamic Variables

Hi:
PROC MEANS supports the use of the automatic variable reference _NUMERIC_ -- which might do what you want. You can read about how the _TYPE_ variable is created and how you can use it to determine which variables contributed to the calculated statistics.

At some point, unless you generate the TABULATE or REPORT code with a macro program, you will have to use the variable names or report item names in the code, such as in a TABLE statement or in a DEFINE statement.

cynthia

[pre]

proc means data=sashelp.class mean;
class sex;
var _numeric_;
output out=work.mnout mean=;
run;

proc print data=work.mnout;
title '_type_ 0 is the overall mean; _type_ 1 is the class variable mean';
run;

proc means data=sashelp.class mean;
class name;
var _numeric_;
output out=work.namout mean= ;
run;

proc print data=work.namout;
title 'Using Name as the CLASS variable';
run;
[/pre]
Super Contributor
Super Contributor
Posts: 365

Re: Mean/Average Value for Dynamic Variables

Hello 1239,

This is a solution:
[pre]
data i;
input Project $ Country $ T1 T2;
datalines;
A INDIA 9 8
A US 7 6
A UK 5 4
run;
data i1;
input Project $ Country $ K1 K2 K3;
datalines;
B INDIA 1 2 3
B US 5 4 7
B UK 6 8 9
run;
%macro a(in=,out=,prefix=);
proc means data=&in noprint;
output out=mean(drop=_Smiley Happy mean=;
var &prefix:;
run;
data &out;
length Project $20;
set &in mean end=e;
Project=CATX(" ",Project,Country);
if e then Project="All_COUNTRY";
drop Country;
format &prefix: 5.1;
run;
%mend a;
%a(in=i,out=r,prefix=T)
%a(in=i1,out=r1,prefix=K)
[/pre]
Sincerely,
SPR
Super User
Posts: 10,047

Re: Mean/Average Value for Dynamic Variables

[pre]

data temp;
input Project $ Country $ T1 T2 ;
cards;
A INDIA 9 8
A US 7 6
A UK 5 4
;
run;
proc summary data=temp ;
var _numeric_;
output out=all_mean(drop=_type_ _freq_) mean= ;
run;
data want;
set temp end=last;
output;
if last then do;
set all_mean;
country='All_Country';
output;
end;
run;
[/pre]


Ksharp
Contributor
Posts: 55

Re: Mean/Average Value for Dynamic Variables

Hi Ksharp.

Thanks for your help. Its working fine but only thing is i need to format it.

Currently the output is coming as ALL_COUNTRY 7.766666667 7.166666667
so on.

I need output as ALL_COUNTRY 7.7 7.1

It should not round off but i need only one digit after decimal.

I tried adding format but no impact on output. Please help.

data temp;
input Project $ Country $ T1 T2 ;
cards;
A INDIA 9 8
A US 7 6
A UK 5 4
;
run;
proc summary data=temp ;
var _numeric_;
format _numeric_ 8.1;
output out=all_mean(drop=_type_ _freq_) mean= ;
run;
data want;
set temp end=last;
output;
if last then do;
set all_mean;
country='All_Country';
output;
end;
run;
Super User
Posts: 10,047

Re: Mean/Average Value for Dynamic Variables

OK.
It is easy.
[pre]


data temp;
input Project $ Country $ T1 T2 ;
cards;
A INDIA 9.5634 8.3234
A US 7.4334 6.5434
A UK 5.4343 4.5634
;
run;
proc summary data=temp ;
var _numeric_;
output out=all_mean(drop=_type_ _freq_) mean= ;
run;

proc format ;
picture myfmt
low-high='0009.9'
;
run;
data want;
set temp end=last;
format _numeric_ myfmt.;
output;
if last then do;
set all_mean;
country='All_Country';
output;
end;
run;
[/pre]


Ksharp
Contributor
Posts: 55

Re: Mean/Average Value for Dynamic Variables

Hi Ksharp,

Thanks a lot for your help. Its working as per my requirement.

Hi SPR and Cynthia@sas.Thanks for your help.
Ask a Question
Discussion stats
  • 8 replies
  • 398 views
  • 0 likes
  • 4 in conversation