I have the following code that perfectly works:
proc iml;
a = {1,2,3,4,0};
b = {1 1, 1 2, 2 3,3 1, 2 2};
c = a||b;
d = full(c);
print a,b,c,d;
quit;
Now, I'm trying to the same thing with one row of my data but getting errors. My code is:
proc iml;
use test(keep = TR_:);
read all var _num_ into x [colname=vn];
b = t(scan(compress(vn,'S'),2,'t_') // scan(compress(vn,'S'),3,'t_'));
a = t(x);
c = a||b;
d = full(c);
print x, a, b, c, d;
quit;
I can't somehow get the matrix 'c' correctly, it is throwing some error. Can anyone please point out where I'm going wrong?
The test file is following:
Year | Segment1 | TR_S6_S5 | TR_S7_S5 | TR_S6_S7 | TR_S7_S6 | TR_S7_S8 | TR_S6_S8 | TR_S3_S1 | TR_S2_S1 | TR_S2_S3 | TR_S3_S2 | TR_S3_S4 | TR_S2_S4 | TR_S6_S2 | TR_S7_S3 | TR_S5_S1 | TR_S8_S4 | TR_S2_S6 | TR_S3_S7 |
2017 | ABC | 0.001 | 0.3 | 0 | 0 | 0.02 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Got it. The reason was actually the fact that I didn't do num(scan(compress(vn,'S'),2,'t_')) so b was character matrix in disguise of numeric.
Following code works:
proc iml;
use test(keep = TR_:);
read all var _num_ into x [colname=vn];
b = t(num(scan(compress(vn,'S'),2,'t_')) // num(scan(compress(vn,'S'),3,'t_')));
a = t(x);
c = a||b;
d = full(c);
print x, a, b, c, d;
quit;
Paste the code and messages from the Log into a code box opened with the forum {I} menu icon.
Got it. The reason was actually the fact that I didn't do num(scan(compress(vn,'S'),2,'t_')) so b was character matrix in disguise of numeric.
Following code works:
proc iml;
use test(keep = TR_:);
read all var _num_ into x [colname=vn];
b = t(num(scan(compress(vn,'S'),2,'t_')) // num(scan(compress(vn,'S'),3,'t_')));
a = t(x);
c = a||b;
d = full(c);
print x, a, b, c, d;
quit;
@ss59 glad you found your answer 🙂
1 small thing that itches every IML programmer about this code is that you should always remember to close your data sets when you are done reading from them. So add a
close test;
statement after your read statement.
For a (5) reason(s) why, read Five Reasons to CLOSE Your Data Sets
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.