Help using Base SAS procedures

Computing means...without proc MEANS

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Computing means...without proc MEANS

Hi there!

 

I am trying ot compute means across every row of data (say 3000) that i have in my dataset. There are 3000 rows and 50 columns - and the mean is looking ot average 8 of the 50 columns across 3000 lines. 

 

Below is the code i have written, but it collapses the data into 1 average, which is not what i am looking for. How do i get it to average across 8 variables for each of the 3000 rows? 

 

Would proc means work here? When i tried it, it appeared as though that would would collapse the data

 

 

data ccm_crsp_monthly4;

array x[8] DP_Rank PPE_Rank PS_Rank TPS_Rank CF_Rank TFO_Rank PSR_Rank DPS_Rank;

array y[6] DS_Rank RO_Rank PS_Rank TPS_Rank PR_Rank XPS_Rank;

SixVar=mean(of y[*]);

EightVar=mean(of x[*]);

Run;

 

 

More failed attempts - proc means: 

 

proc means DATA=ccm_monthly;

class date permno;

var DS_Rank PPE_Rank PS_Rank TPS_Rank XS_Rank XRS_Rank;

output

out=cc_monthly4(drop=_TYPE_ _FREQ_) mean=SixVariable;

run;

 

 

Thsi should be a really simple things to do...lol!

 

 


Accepted Solutions
Solution
‎05-20-2017 01:14 AM
Super User
Posts: 11,343

Re: Computing means...without proc MEANS

You really should provide a small example data set and what the result for that example data should be.

 

Your comment "but it collapses the data into 1 average" is not very descriptive since the code you show would create 2 variables with the means of the two arrays for each row of data. If that isn't what you want then you need to show a very explicit example. The example need not include all 14 variables, probably the first 3 variables of each array and values that are not the same for each variable and row.

 

If you have a data then Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

View solution in original post


All Replies
Solution
‎05-20-2017 01:14 AM
Super User
Posts: 11,343

Re: Computing means...without proc MEANS

You really should provide a small example data set and what the result for that example data should be.

 

Your comment "but it collapses the data into 1 average" is not very descriptive since the code you show would create 2 variables with the means of the two arrays for each row of data. If that isn't what you want then you need to show a very explicit example. The example need not include all 14 variables, probably the first 3 variables of each array and values that are not the same for each variable and row.

 

If you have a data then Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Regular Contributor
Posts: 222

Re: Computing means...without proc MEANS

Where is you Set Statement?

data ccm_crsp_monthly4;

array x[8] DP_Rank PPE_Rank PS_Rank TPS_Rank CF_Rank TFO_Rank PSR_Rank DPS_Rank;

array y[6] DS_Rank RO_Rank PS_Rank TPS_Rank PR_Rank XPS_Rank;

/* SET <lib.>dataset; */
SixVar=mean(of y[*]);

EightVar=mean(of x[*]);

Run;
Trusted Advisor
Posts: 1,934

Re: Computing means...without proc MEANS

Below is the code i have written, but it collapses the data into 1 average, which is not what i am looking for. How do i get it to average across 8 variables for each of the 3000 rows?

Sometimes words across the internet fail to have the intended meaning, I just don't understand this sentence, and it would be extremely helpful if you could show us an example of what this sentence really means.

 

But, in general, if you want to average numbers left-to-right (or right-to-left) within an observation, you need to use the MEAN function in a datastep.

 

If you want to average down columns, you can use PROC MEANS. The only way to get PROC MEANS to average right-to-left within a row is to first transpose the data set so that what used to be a row is now a column.

 

 

Occasional Contributor
Posts: 19

Re: Computing means...without proc MEANS

Posted in reply to PaigeMiller

Thank you for your response, it turns out my code worked exactly as intended - i am not sure why it was collapsing the data int he begiining. But upon clearing sas and re-running my code- it turned out well. Thank you! 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 197 views
  • 0 likes
  • 4 in conversation