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, 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 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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")];

 

View solution in original post

3 REPLIES 3
IanWakeling
Barite | Level 11

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.

Rick_SAS
SAS Super FREQ

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")];

 

Jonison
Fluorite | Level 6

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

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!

Multiple Linear Regression in SAS

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 3 replies
  • 560 views
  • 0 likes
  • 3 in conversation