BookmarkSubscribeRSS Feed
Not applicable
Hi Guys:

Thanks for taking the time to view my thread.

So here's my dilemma. I need to create a two dimensional matrix based upon the total count of replicates given in a clinical trial sample. Now all samples have barcodes associated with it. So if a sample is identified as X1 or X2 then a proc feq will reveal the total count of that sample through out the trial. This count can range any where between 1 - 12 reps.

So for this example lets say the incoming sample count for barcode X1 is given as 4. Therefore the resultant matrix should be a 4x16. ie: 4 columns and 16 rows. The row is calculated as 2**4. So if the rep is 5 then the matrix is 5x32 ie: 5 cols and 2^5 or 32 rows. So on and so forth.. the maximum rep as mentioned earlier is 12 which means 12 cols and 4096 rows of all combination's of 0's and 1's.

The data in all of the dynamically created matrix should be all possible combination's of 0's and 1's for that many rows.

In this example I like to present the 4x16 matrix. In this matrix the data is as follows where the first row always begins with 0's.

Data for 4x16 matrix:

0 0 0 0
0 0 0 1
0 0 1 1
0 1 1 1
0 1 1 0
0 1 0 0
0 0 1 0
1 1 1 1
1 1 1 0
1 1 0 0
1 0 0 0
1 0 0 1
1 0 1 1
0 1 1 1
1 0 1 1
1 1 0 1

As you can see the combination of 0's and 1's begins with the very first row of all 0's. In this example, since the incoming rep count is 4, the first row has 4 0's. If the rep count is 5 then we have five 0's, rep count 6 six 0's and so forth.

My question is, what is the best way to go about creating such matrix-es. I know we can do this using array statement but I'm not sure if array is the way to go or to use proc IML procedures. I'm completely new to proc IML so I've yet to learn the many features of IML procedures.

Your help is much appreciated.

Many thanks,
This is an interesting question! I assume that the order of the rows are unimportant, since I don't see any pattern to the rows that you typed for your 4x16 example. I think the key to solving this problem is to find a systematic ordering to the rows so that it becomes apparent how to produce the matrix for RepCount=3, 4, 5, and so on.

I have posted a solution to this problem on my SAS/IML blog:

The following URL jumps directly to the solution:
Not applicable
Hi Rick,

Thanks a million for your reply. Your solution is very interesting and was kinda the kind of way I was hoping to go, but you give more insight into the problem.

I was able to reach a point where I could create a matrix dynamically using the array statement but I was and still not sure how to populate the rows and columns. What I did was use the 3x8 example and create a txt file of the 0's and 1 combination and tried to read that file via input.

%macro test(rep);

%let rep=3;
%let rowCnt=%eval(2**&rep);
%let str=;

dm 'clear log' log;

data autoTest_&rep.x&rowCnt(keep=r1c1-r1c&rep);
array matrixA{&rowCnt,&rep }
%do i=1 %to &rowCnt;
%if &i eq 1 %then %do; %let str= r1c1 - r1c&rep; %end;
%else %if &i lt %eval(&i+1) %then %do; %let str= &str r&i.c1 - r&i.c&rep; %end;
%else %if &i eq &rowCnt %then %do; %let str = &str r&i.c1-r&i.c&rep; %end;
infile 'C:\SAS\Matrix\rawdata\matrix3x8.txt' dsd dlm=' ' truncover lrecl=256 termstr=LF;
input r1c1-r1c&rep;


data autoTest_&rep.x&rowCnt;
set autoTest_&rep.x&rowCnt;

%let renames=;
%do i=1 %to &rep;
%if &i eq 1 %then %do; %let renames=r1c1 = col1; %end;
%else %if &i gt 1 and &i lt %eval(&i+1) %then %do; %let renames = &renames r1c&i = col&i; %end;
%else %if &i eq &rep %then %do; %let renames = &renames r1c&rep = col&rep; %end;
rename &renames;

%mend test;

The contents of the text file are as follows:

0 0 0
0 0 1
0 1 0
0 1 1
1 1 1
1 1 0
1 0 0
1 0 1

Once again, Rick, Thanks a bunch...

I can't tell from your reply if you've solved your problem or if you are asking another question.

If you use the IML solution I posted, then you don't need any DATA step/macro code. If you don't want to use IML and you have a question about the DATA step, then is a good place to seek additional help.
Calcite | Level 5

Here is a online guide that can help you on 2d data matrix creating. Recently, I have tested my friend recommended image barcode creating suite, which enables users to creare and generate both linear and 2d barcodes on image and document page, including data matrix generation. I am a programmer using visual basic, so I used its barcode creation tools, and I can share the 2d datamatrix barcode generation sample codes with you, you can simply copy these codes within your vb project or convert them into c# codes if you are developer using Visual C#. Of cause, you need to make some specifications as your needs. Besides, if you still  want to generate other popular 2d barcodes, like qr code and pdf 417, see the related post article.

Dim reImage As REImage = REFile.OpenImageFile("c:/Sample.png", New PNGDecoder())

Dim barcode As New DataMatrix()

'create a datamatrix barcode

  1. barcode.Data = "123456789"

'encode data into Data Matrix barcode 

  1. barcode.X = 5F

' set module size

  1. barcode.Resolution = 96

'set barcode printing resolution

  1. barcode.Rotate = Rotate.Rotate0

'set rotate

  1. barcode.DrawBarcode(reImage, 0, 0)

'draw barcode on REImage with location x and y

  1. REFile.SaveImageFile(reImage, "c:/datamatrix.png", New PNGEncoder())


I don't understand any of these replies about "dynamic matrix creation."  The SAS/IML language is a full programming language with the ability to allocate sapce for matrices, loop over elements, create mathematical formulas, make assignments, etc.

I don't understand why someone would need to use C#, C++, or any other external language to create a matrix.  If I am missing something, please explain what you want to do and I'll try to show how you can do it in SAS.

Super User

You are enumerating all the combinations . Check GRAYCODE() . You can use it in either IML or Data step .

data _null_;

array x[4];




do i=1 to nsubs;

rc=graycode(k, of x

  • );
  • put i 5. +3 k= x

  • +3 rc=;
  • end;


    Xia Keshan



    Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

    If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

    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
    • 7 replies
    • 5 in conversation