SAS/IML Software and Matrix Computations

Statistical programming, matrix languages, and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

I am looking to take a column off an existing dataset (Weight in the example below), do logic on it in IML and then add a new column to the existing data set (or create a new dataset with all the columns from the original and the new column).   Thank you.

 

proc iml;
ds = "Sashelp.Class";
use (ds);
read all var _NUM_ into X;
close (ds);

 

...do some vector logic using Weight...

 

...add the new column vector back onto the original dataset...

 

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

The easiest way is to create a new data set that has the new column(s) and then merge with the original data:

 

proc iml;
ds = "Sashelp.Class";
use (ds);
read all var _NUM_ into X[c=VarNames];
close (ds);

print varNames;

weight = X[,'Weight'];
NewWeight = 2*weight;

create NewCol var "NewWeight";
append;
close;
QUIT;

data newClass;
merge Sashelp.Class NewCol;
run;

proc print data=NewClass; run;

View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

The easiest way is to create a new data set that has the new column(s) and then merge with the original data:

 

proc iml;
ds = "Sashelp.Class";
use (ds);
read all var _NUM_ into X[c=VarNames];
close (ds);

print varNames;

weight = X[,'Weight'];
NewWeight = 2*weight;

create NewCol var "NewWeight";
append;
close;
QUIT;

data newClass;
merge Sashelp.Class NewCol;
run;

proc print data=NewClass; run;
IanWakeling
Barite | Level 11

Why not use a table?

 

proc iml;
  t = TableCreateFromDataSet("Sashelp", "Class");
  NewWeight = TableGetVarData(t, "Weight")#2;
  call TableAddVar(t, "NewWeight", NewWeight);
  call TableWriteToDataSet(t, "work", "NewClass");
quit;
proc print data=NewClass;
run;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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