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

Hello, expert, I have a matrix

 

IDcol2col3col4
10.25430.3763630.306857
20.0106510.2593330.073882
30.3718740.2585920.634631
4-0.229210.084731-0.39482
50.5200230.5536740.360524

 

can you please suggest how to do calculation for a set of cells in the table.

For example, calculate the average and std of row 2 and column  3 and 4, e.g. [2, 3:4].

Many thanks, is there any procedure would have such function?

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Something like this:

 

data have;
 set sashelp.class;
 a=1;b=2;c=3;
run;

%let row=2;
%let col=2:5 ;
proc iml;
use have;
read all var _num_ into x;
close; 	

avg=mean(colvec(x[&row,&col]));
std=std(colvec(x[&row,&col]));

print avg,std;
quit;

View solution in original post

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

Do you have SAS/IML?

Jonison
Fluorite | Level 6
Thanks, I think I have the SAS/IML.
PaigeMiller
Diamond | Level 26

Does this have to be done in a matrix, or can it be done in a SAS data step? Is it always columns 3 and 4 that is of interest, or can that change depending on various different circumstances?

 

Please don't say "I think I have...", please say either: you have it or you do not have it. 

--
Paige Miller
Jonison
Fluorite | Level 6

the column number is fixed always ID, x,x1-x7,

but the row number will change, due the updating database on daily basis.

 

Thanks.

PaigeMiller
Diamond | Level 26

@Jonison wrote:

the column number is fixed always ID, x,x1-x7,

but the row number will change, due the updating database on daily basis.


How does the programmer know which columns and rows to use for computing the mean?

--
Paige Miller
Jonison
Fluorite | Level 6

would you please show me some example of using IML to do calculation? not familiar the syntax,

 

Thanks

 

Ksharp
Super User

Something like this:

 

data have;
 set sashelp.class;
 a=1;b=2;c=3;
run;

%let row=2;
%let col=2:5 ;
proc iml;
use have;
read all var _num_ into x;
close; 	

avg=mean(colvec(x[&row,&col]));
std=std(colvec(x[&row,&col]));

print avg,std;
quit;
Jonison
Fluorite | Level 6

Thanks for your kind help, it is really useful and works perfectly.

Tom
Super User Tom
Super User

Do you have a "matrix" or a dataset?  Assuming you have a dataset then you are asking for:

data want;
  set have;
  where id = 2 ;
  avg = mean(of col3 col4);
  std = std(of col3 col4);
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 9 replies
  • 769 views
  • 0 likes
  • 5 in conversation