Fluorite | Level 6

## input calculated result to new column of table.

Hello, experts,

I have a question about writing calculated results to the a new column at same row.

The code I have is:

proc iml;
use work.exp;
read all var _num_ into x;
close;

do row=1 to 9;
Sum = ssq(colvec(&x[&row, &col]-&x[&row, &bcol]));
SE = &STD*(sqrt(6/7*Sum));
how to write the SE to x[&row, &(col+1)]?

end;

mend;

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Super FREQ

## Re: input calculated result to new column of table.

1. Write and debug a SAS/IML program before you try to put it into a macro.

2. I'm guessing you want to store each value of SE into a column vector and then concatenate that vector as a new column for x:

``````proc iml;
use sashelp.class;
read all var _num_ into x[colname=varNames];
close;

COL = 3; BCOL=1; STD = 2;  /* make up values */

N = nrow(x);
SE = j(N, 1, .);          /* allocate vector for SE */
do row=1 to N;
Sum = ssq(colvec(x[row, COL]-x[row, BCOL]));
SE[row] = STD*(sqrt(6/7*Sum));
end;

*print SE;
/* if you want to add a new column to x, use concatenation */
newX = x || SE;
print newX[colname=(varNames || "SE")];
``````

3 REPLIES 3
Barite | Level 11

## Re: input calculated result to new column of table.

Try replacing '&x' and '&row' with 'x' and 'row' respectively, as I believe these should be IML matrices, not references to macro variables. Syntax like

``x[row, &col+1] = SE``

could be used to write the result to the matrix x if the macro variable &col is defined correctly. You will get better help if you show us a small example of the data you are working on, and show us what warnings or errors occur when your code runs.

SAS Super FREQ

## Re: input calculated result to new column of table.

1. Write and debug a SAS/IML program before you try to put it into a macro.

2. I'm guessing you want to store each value of SE into a column vector and then concatenate that vector as a new column for x:

``````proc iml;
use sashelp.class;
read all var _num_ into x[colname=varNames];
close;

COL = 3; BCOL=1; STD = 2;  /* make up values */

N = nrow(x);
SE = j(N, 1, .);          /* allocate vector for SE */
do row=1 to N;
Sum = ssq(colvec(x[row, COL]-x[row, BCOL]));
SE[row] = STD*(sqrt(6/7*Sum));
end;

*print SE;
/* if you want to add a new column to x, use concatenation */
newX = x || SE;
print newX[colname=(varNames || "SE")];
``````

Fluorite | Level 6

## Re: input calculated result to new column of table.

Super, thank you very much. It works, learned new skill! 😁

From The DO Loop