BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ttaria
Calcite | Level 5
 
 
 

Hello,

 

I was hoping someone could help me with some codes to determine the mean across positive values variables (MPOS). For example, MPOS (Year=2000)= avg (var1+var4)

MPOS (Year=2001)= avg (var2+var3)

MPOS (Year=2002)= avg (var1+var2)

......

Year

Var1

Var2

Var3

Var4

MPOS

2000

23

-10

-5

22

22.5

2001

-1

10

33

-20

21.5

2002

10

20

-30

-10

15

2003

20

-10

10

-10

15

2004

8

10

3

-12

7

2005

10

-5

-23

-11

10

Thanks!
1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Declare var1 through var4 as an array.  Copy only positive elements of that array to a new array.  Use the MEAN  function on that new array:

 

data have;
  input Year Var1 Var2 Var3 Var4 MPOS;
datalines;
2000 23 -10  -5  22 22.5
2001 -1  10  33 -20 21.5
2002 10  20 -30 -10 15
2003 20 -10  10 -10 15
2004  8  10   3 -12  7
2005 10  -5 -23 -11 10
run;
data want (drop=_:);
  set have;
  array var {*} var1-var4;
  array _tmp {4};
  do _i=1 to dim(var);
    _tmp{_i}=ifn(var{_i}>0,var{_i},.);
  end;
  meanvar=mean(of _tmp{*});
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
mkeintz
PROC Star

Declare var1 through var4 as an array.  Copy only positive elements of that array to a new array.  Use the MEAN  function on that new array:

 

data have;
  input Year Var1 Var2 Var3 Var4 MPOS;
datalines;
2000 23 -10  -5  22 22.5
2001 -1  10  33 -20 21.5
2002 10  20 -30 -10 15
2003 20 -10  10 -10 15
2004  8  10   3 -12  7
2005 10  -5 -23 -11 10
run;
data want (drop=_:);
  set have;
  array var {*} var1-var4;
  array _tmp {4};
  do _i=1 to dim(var);
    _tmp{_i}=ifn(var{_i}>0,var{_i},.);
  end;
  meanvar=mean(of _tmp{*});
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ttaria
Calcite | Level 5

This worked out for me.

Thank you!!

novinosrin
Tourmaline | Level 20
data have;
  input Year Var1 Var2 Var3 Var4 ;
datalines;
2000 23 -10  -5  22 22.5
2001 -1  10  33 -20 21.5
2002 10  20 -30 -10 15
2003 20 -10  10 -10 15
2004  8  10   3 -12  7
2005 10  -5 -23 -11 10
run;

data want;
set have;
_count=0;
_temp=0;
array t(*) var:;
do _n_=1 to dim(t);
if t(_n_)>0 then do;_temp+t(_n_);_count+1;end;
end;
MPOS=_temp/_count;
drop _:;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 516 views
  • 1 like
  • 3 in conversation