BookmarkSubscribeRSS Feed
Rasheed
Calcite | Level 5

Ok I try

I would be grateful if you could pleas tell how could I save each matrix in different variable I am using following program

r = 1:2;  /* row numbers to extract from x */

do boot = 1 to 2;

  y = x[r, 2:3];

  < do stuff here with the 2x2 matrix y >

  r = r + 2;  /* increment row numbers for next boot */

end;

Plz help me in this regard

IanWakeling
Barite | Level 11

If you are prepared to sacrifice readability of code, then you can create a character matrix y that contains the names of each matrix in your list. Then you can use the VALUE() function and the VALSET() call every time you want to refer to those matrices.

  x = {
1 0.0036813 0.0030405 ,
1 0.0030405 0.0032935 ,
2 0.0015928 0.0017098 ,
2 0.0017098 0.0020805 };

  y = 'y_1' : 'y_2';  /* define matrix NAMES */

  r = 1:2;
  do k = 1 to 2;
    call valset( y, x[r, 2:3]);      /* create the matrix y_k */
    print (value( y ));              /* print y_k */
    call valset( y, value(y)#2 ); /* multiply by 2 */
    print (value( y ));              /* print again */
    r = r + 2;
  end;

I can see Rick's point that it will be easier to store all the results in once place, however I would prefer to have a choice and for there to be an easier way to create a list of matrices, where each element can be a matrix of any size.

Rasheed
Calcite | Level 5

Thanks Ian I used your code it worked fine for me thank you again Now one thing is more I want to convert these separated matrices into separate data sets and using following command but not working pls guide. Here I want to convert matrices C_1, C_2 into data sets CD_1 CD_2

do k = 1 to 2;

call execute('create ' + CD +'  from value( C);');                                                                                                                                                                                                 

append from  value(C);                                                                                                                                                                                                                                         

call execute('close ' + CD + ';' );

end;

IanWakeling
Barite | Level 11

In this context (CREATE and APPEND), you do not want the value of the matrix, just its name.  So you would need to modify the code as follows:

do k = 1 to 2;

  call execute('create ' + CD +'  from ' + C + ';');

  call execute('append from ' + C + ';') ;

  call execute('close ' + CD + ';' );

end;

and because of the call execute, you would have to use this code within a module.  Be warned this could get messy if you are creating hundreds of data sets,  why not save the original matrix from the beginning of this thread as a SAS data set?

Rasheed
Calcite | Level 5

Thanks again Ian, is there any option from which we can convert into data sets without using module?

IanWakeling
Barite | Level 11

You would have to copy your matrix C to another matrix for each iteration of the loop, as there is no facility to supply an expression for the 'matrix name' that follows the keyword FROM.   For example:

do k = 1 to 2;

  z = value(C);

  create (CD) from z;

  append from z ;

  close (CD);

end;

Rasheed
Calcite | Level 5

When I run the above previous code I got the following error

do k = 1 to 2;

891

892    z = value(C);

893

894    create (CD) from z;

              -

              22

              76

ERROR 22-322: Expecting a name.

ERROR 76-322: Syntax error, statement will be ignored.

895

896    append from z ;

897

898    close (CD);

             -

             22

             200

ERROR 22-322: Syntax error, expecting one of the following: a name, ;.

ERROR 200-322: The symbol is not recognized and will be ignored.

899

900    end;

ERROR: END does not occur within DO group at line=900 col=3.

IanWakeling
Barite | Level 11

I presume you have CD defined with something like:

CD='CD_1':'CD_2';

In which case it might be a version thing as it is working for me in SAS/IML 13.1.  Perhaps Rick could confirm this, and also comment on why an expression was allowed for the data set name, but not for the matrix name.

Rasheed
Calcite | Level 5

There are two matrices C_1 and C_2 and I am using following code without module to convert them in to data sets CD_1 and CD_2

CD='CD_1':'CD_2';

do k = 1 to 2;

  z = value(C);

  create (CD) from z;

  append from z ;

  close (CD);

end;

But still got the same error

Rick_SAS
SAS Super FREQ

Reading and writing data sets whose names are contained in a character matrix is supported beginning in  SAS/IML 12.1 (released with 9.3m2). See http://blogs.sas.com/content/iml/2013/07/29/read-data-sets-array-names.html

Ian: The APPEND statement doesn't support expressions because it is valid syntax to specify a character matrix, in which case the APPEND statement writes that data to the data set.

Rick_SAS
SAS Super FREQ

If you wish to pursue Ian's suggestion, there is more information about VALUE and VALSET in the article "Indirect assignment: How to create and use matrices named x1, x2,..., xn"

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
  • 25 replies
  • 1641 views
  • 0 likes
  • 4 in conversation