BookmarkSubscribeRSS Feed
desireatem
Pyrite | Level 9

Thank a lot , I am trying to enter my complete data into metric. I know some variable won't change like

I know some variable won't change
like
time censored status y Frequency but COL1-COLM will change
base on censoring. Frequency is the number censored. So since my COL1-COLM will
vary, I just want to give it a generic number , so I

define M=frequency[1];

The number of columns for COL is always equal to the first element in the variable frequency.

So,if M=frequency[1];=3, then we will have COL1, COL2 and COL3. I want to define it base

M=frequency[1];

Any Suggestion?

prociml;

use
impute;

M=frequency[1];

do j=1 to M;

read
all
var{time censored status y Frequency COL1-COLM} into DM;

end;

close;

Status=DM[,5]; frequency =DM[,4]; survival1 =DM[,3]; Censored= DM[,2]; time = DM[,1]; COLj=DM[,] /*Here
I have no idea******/
; /n = nrow(DM);











12 REPLIES 12
Rick_SAS
SAS Super FREQ

Use two READ stmts. For the first READ statement, read the variables that don't change. Figure out the value of m.

Then use the "colon operator" technique in this article How to create column names for matrices - The DO Loop

to form the name of the variables:

varNames = "x1":("x"+strip(char(m)));

desireatem
Pyrite | Level 9

The value for M=Frequency[1]. That is M is always equal to M=Frequency[1], the first element in frequencybut this m will vary from in each simulation.

The column are name COL1-COLM, where M=M=Frequency[1]. I know there names, I just want a generic read statement that will accomodata COL1-COL4 in one matrix and COL1-COL6 in anothere

proc iml;

    use Complete;

    read all var _all_ into DM;   

    close;

    frequency =DM[,4]; y =DM[,3]; Censored= DM[,2]; time = DM[,1];  Cols=DM(n,m);                                                            /*Survivalival is second column*/  /*time is first column*/

    n = nrow(DM);

M=frequency[1];

Completed=J(nrow(survival1),M+4,1);

do j=1 to M;

proc iml;

    use Complete;

    read all var _all_ into DM;   

    close;

    frequency =DM[,4]; y =DM[,3]; Censored= DM[,2]; time = DM[,1];  Cols=DM(n,m);                                                            /*Survivalival is second column*/  /*time is first column*/

    n = nrow(DM);

M=frequency[1];

Completed=J(nrow(survival1),M+4,1);

do j=1 to M;

read all var{COLj}

Hutch_sas
SAS Employee

I think what you need to do is add one more line before your read statement to create the variable column names, in the manner Rick advised, and then use that as an expression for the VAR clause, as illustrated in this simple code snippet::

fixed_names = {"var_a" "var_b" "var_c"}; /* these are the variables you always read */

M = frequency[1];

cols = "COL1":("COL"+strip(char(M))); /* computes cols = {"COL1" "COL2" ... "COLM"} */

read all var (fixed_names || cols) ; /* or if there are no fixed_names this time just:

                                      *  read all var cols; */

Does this answer your question?

desireatem
Pyrite | Level 9

I am tried what you said this is what I did;

proc iml;

      use Complete;

    read all var _all_ into DM;    

      close;

      fixed_names = {"frequency" "Censored" "time" "y"};

      M=frequency[1];

      cols = "COL1"Smiley Sad"COL"+strip(char(M)));

      read all var(fixed_names || cols); 

print ;

This the log file;

 

1931 proc iml;

NOTE: IML Ready

1932 use Complete;

1933 read all var _all_ into DM;

1934 close;

NOTE: Closing WORK.COMPLETE

1935 fixed_names = {"frequency" "Censored" "time" "y"};

1936 M=frequency[1];

ERROR: (execution) Matrix has not been set to a value.

operation : [ at line 1936 column 16

operands : frequency, *LIT1002

Frequency 0 row 0 col (numeric)

 

*LIT1002 1 row 1 col (numeric)

1

statement : ASSIGN at line 1936 column 5

1937 cols = "COL1":("COL"+strip(char(M)));

ERROR: (execution) Matrix has not been set to a value.

operation : CHAR at line 1937 column 36

operands : M

M 0 row 0 col (type ?, size 0)

 

statement : ASSIGN at line 1937 column 5

1938 read all var(fixed_names || cols);

ERROR: No data set is currently open for input.

statement : READ at line 1938 column 5

1939 print ;

-

22

76

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,

a numeric constant, a datetime constant, a missing value, ',', /.

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

Hutch_sas
SAS Employee

I am not sure exactly what you want to do. There were 2 errors. First, you need to define frequency after your first read. The next error was that you need to re-open the data set since you closed it:

proc iml;

      use Complete;

     read all var _all_ into DM;    

      close;

      frequency = DM[,4];

      fixed_names = {"frequency" "Censored" "time" "y"};

      M=frequency[1];

      cols = "COL1" : ("COL"+strip(char(M)));

   use Complete;

      read all var(fixed_names || cols) into test; 

print test;

If you are only going to use frequency[1], then you only need to read in the first record in your first read, not all records.

desireatem
Pyrite | Level 9

Thanks foryou help. just started using proc iml on last Thursday. This is mymodification base on your statement;

proc iml;

  use Complete;

  read all var _all_ into DM; 

M=frequency[1];

fixed_names = {"frequency""Censored" "time""y"};

cols = "COL1": ("COL"+strip(char(M)));

      read all var(fixed_names || cols) into testA; 

        close;

print testA;

LOG FILE;

261  proc iml;


NOTE: IML Ready


262       use Complete;


263       read all var _all_ into DM;


264        M=frequency[1];


ERROR: (execution) Matrix has not been set to a value.



operation : [ at line 264 column 18


operands  : frequency, *LIT1001



Frequency      0 row       0 col     (numeric)



 


*LIT1001      1 row       1 col     (numeric)



         1



statement : ASSIGN at line 264 column 7


265        fixed_names = {"frequency" "Censored" "time" "y"};


266        cols = "COL1" : ("COL"+strip(char(M)));


ERROR: (execution) Matrix has not been set to a value.



operation : CHAR at line 266 column 40


operands  : M



M      0 row       0 col     (type ?, size 0)



 


statement : ASSIGN at line 266 column 7


267        read all var(fixed_names || cols) into testA;


268        close;


NOTE: Closing WORK.COMPLETE


269


270  print testA;

Hutch_sas
SAS Employee

You left out

frequency = DM[,4];

desireatem
Pyrite | Level 9

Thanks foryour help Hutch, I just started programming in imland having issues. I did what you said but still got problem;

prociml;

use
Complete;

read
all
var _all_ into
DM; 

frequency = DM[,4];

M=frequency[1];

fixed_names = {"frequency""Censored" "time""y"};

cols = "COL1"Smiley Sad"COL"+strip(char(M)));

read all var(fixed_names || cols) into
testA; 

close;

print testA;

2382  proc iml;


NOTE: IML Ready


2383  use Complete;


2384   read all var _all_ into DM;


2385   frequency = DM[,4];


2386  M=frequency[1];


2387  fixed_names = {"frequency" "Censored" "time" "y"};


2388  cols = "COL1": ("COL"+strip(char(M)));


ERROR: (execution) Invalid operand to operation.



operation : : at line 2388 column 14


operands  : *LIT1004, _TEM1003



*LIT1004      1 row       1 col     (character, size 4)



COL1



_TEM1003      1 row       1 col     (character, size 15)

Hutch_sas
SAS Employee

Your frequency must be a floating point number. I thought from your earlier posts that frequency would be an integer, and that was the number of columns you expected. You need to set M to the (integer) number of columns that you need to read in.

desireatem
Pyrite | Level 9

Thanks , a lot, please do you know how I could  set M to the (integer) number of columns that you need to read in. I do not know how to do that, googled but no helpful answer

Hutch_sas
SAS Employee

You can always truncate the floating point number to an integer with the int() function, i. e. M = int(frequency[1]) or round it to the nearest int with M=round(frequency[1]) or round it up with M=ceil(frequency[1]). I can't know which, if any of these will work with your data set.

desireatem
Pyrite | Level 9

THanks Hutch, M = int(frequency[1]) works well. Programis good to go. Your help has been invaluable.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 12 replies
  • 2730 views
  • 7 likes
  • 3 in conversation