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
Rhodochrosite | Level 12
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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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
  • 4 replies
  • 1076 views
  • 0 likes
  • 4 in conversation