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

Hi All,

I need to take an average of samples1 to 10 for respective treatment and replication and then would like to create a new variable of a treatment average (I have already sorted the data for replications and treatments). How can I achieve this by SAS coding, please?. It looks straight forward in excel but I am new to SAS and would  like to learn it. Thank you in advance.

 

Replication (1-3) Treatment (1-18) Sample1-10
1 1 values
2 2  
3 3  

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Assuming your sample variables are named SAMPLE1, SAMPLE2, .... SAMPLE9 SAMPLE10, then

 

data want;

   set have;

   sample_average=mean(of sample1-sample10);

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

4 REPLIES 4
Reeza
Super User

Do you have ten variables, Sample1-Sample10 that you need an average for each line? Or are you calculating the average using data from multiple rows? 

 

If 10 variables

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245914.htm

 

If multiple rows

http://support.sas.com/training/tutorial/studio/summary-statistics.html

 

It also depends on what you want for output, which you haven't specified. 

mkeintz
PROC Star

Assuming your sample variables are named SAMPLE1, SAMPLE2, .... SAMPLE9 SAMPLE10, then

 

data want;

   set have;

   sample_average=mean(of sample1-sample10);

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

--------------------------
fridge_wpg
Obsidian | Level 7
Hi,

I used the codes you suggested. It is not working. Here is the error I get:
ERROR 180-322: Statement is not valid or it is used out of proper order.

My program code is:
data yield; set farmsys.exp38;
proc sort data=yield; by Rep Trt YLD_P1 - YLD_P10;
Avg = mean(OF YLD_P1 - YLD_P10);
proc print data=yield;
run;



that is how the data is tabulated:


Rep

Plot

Trt

YLD_P1

YLD_P2

YLD_P3

YLD_P4

YLD_P5

YLD_P6

YLD_P7

YLD_P8

YLD_P9

YLD_P10

R1

101

15

197.3

53.3

202.3

171.6

202.8

200.5

122.4

190.8

190.1

180.9

R1

102

10

131.4

99.5

135.5

191.5

.

151.4

177.4

194.3

187.9

94.5






Thank you very much for your input on this.



fridge




mkeintz
PROC Star

Data transfomations can generally only be done in a DATA step or PROC SQL.  DefinitelyNOT in a proc sort.

 

SAS is a procedure-oriented tool (some call it a language).

 

So ...

  • Move the "avg=" statement after the SET statement.
  • And termnate the data step (and the proc sort step) with a "run;" statement.
    Not required, but good practice

 

 

Then read something like "The Little SAS Book" to get the basics.  (and run some SAS tutorials.).

--------------------------
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

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

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
  • 4 replies
  • 41537 views
  • 2 likes
  • 3 in conversation