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;
Many thanks for your help.
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")];
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.
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")];
Super, thank you very much. It works, learned new skill! 😁
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.