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

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!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

4 REPLIES 4
ballardw
Super User

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.

AhmedAl_Attar
Ammonite | Level 13
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;
PaigeMiller
Diamond | Level 26
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.

 

 

--
Paige Miller
Mila
Obsidian | Level 7

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! 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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