BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ss59
Obsidian | Level 7

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:

YearSegment1TR_S6_S5TR_S7_S5TR_S6_S7TR_S7_S6TR_S7_S8TR_S6_S8TR_S3_S1TR_S2_S1TR_S2_S3TR_S3_S2TR_S3_S4TR_S2_S4TR_S6_S2TR_S7_S3TR_S5_S1TR_S8_S4TR_S2_S6TR_S3_S7
2017ABC0.0010.3000.020000000000000
1 ACCEPTED SOLUTION

Accepted Solutions
ss59
Obsidian | Level 7

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;

 

 

View solution in original post

3 REPLIES 3
ballardw
Super User

Paste the code and messages from the Log into a code box opened with the forum {I} menu icon.

 

 

 

ss59
Obsidian | Level 7

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;

 

 

PeterClemmensen
Tourmaline | Level 20

@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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 844 views
  • 3 likes
  • 3 in conversation