Statistical programming, matrix languages, and more

How to delete all columns where the col (x, 1)=0, that is the first number in the column is zero

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

How to delete all columns where the col (x, 1)=0, that is the first number in the column is zero

This is the code that I am using;

    SurvivalM=J(nrow(survival1),1,1);

             do i = 1 to n;

                if censored=1 then SurvivalM=survival1; else if censored[1]=0 then SurvivalM=survival1/*****Create a 1 by N -matrix of Survival ****/;

            end;

   

      print SurvivalM;

    InverseC=J(nrow(survival1),1,1);

          do i = 1 to n;

      if censored=1 then InverseC=1/survival1; else if censored[1]=0 then InverseC=0/*****survivalC is the imputed censored survival and set the real Survivalval to 1**/;

   

     *InverseCC=J(nrow(survival1),1,1);

end;

      print InverseC;

Big=SurvivalM*t(InverseC);

print BIG;

/*** for this code I want to delete all colums of BIG where row1 and col1 are zero ***/;

BIGG=J(nrow(BIGG),20,1);

do 1 to n;

if col[i,1]=0 then delete;

end;

print BIGG;

statement : ASSIGN at line 4338 column 1

4339  do 1 to n;

         -

         22

         200

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

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

4340  if col[i,1]=0 then delete;

4341  end;

ERROR: END does not occur within DO group at line=4341 col=1.

4342  print BIGG;

ERROR: Matrix BIGG has not been set to a value.

statement : PRINT at line 4342 column 1


Accepted Solutions
Solution
‎02-02-2013 01:33 AM
Frequent Contributor
Posts: 122

Re: How to delete all columns where the col (x, 1)=0, that is the first number in the column is zero

Use something like:

reduced=BIG[, loc( BIG[ 1,]^= 0 )];

so use the result of the loc function to pick out the list of columns required.  The same technique can be used to remove zeros from the vector in one of your other questions.

Ian.

View solution in original post


All Replies
Super Contributor
Posts: 271

Re: How to delete all columns where the col (x, 1)=0, that is the first number in the column is zero

I created this matrix BIG

ROW1001.08754210000000000004.10158730000
ROW2001.0303030000000000003.88571430000
ROW30010000000000003.77142860000
ROW4000.9696970000000000003.65714290000
ROW5000.90909090000000000003.42857140000
ROW6000.84848480000000000003.20000
ROW7000.78787880000000000002.97142860000
ROW8000.72727270000000000002.74285710000
ROW9000.66666670000000000002.51428570000
ROW10000.60606060000000000002.28571430000
ROW11000.54545450000000000002.05714290000
ROW12000.48484850000000000001.82857140000
ROW13000.42424240000000000001.60000
ROW14000.36363640000000000001.37142860000
ROW15000.30303030000000000001.14285710000
ROW16000.265151500000000000010000
ROW17000.22727270000000000000.85714290000
ROW18000.15151520000000000000.57142860000
ROW19000.07575760000000000000.28571430000
ROW2000000000000000000000

and want to create another matrix BIGG by deleting all the colums that have first element equal zero in BIG to produce the matrix BIGG.

This is the wrong code;

SurvivalM=J(nrow(survival1),1,1); 

             do i = 1 to n;

                if censored=1 then SurvivalM=survival1; else if censored[1]=0 then SurvivalM=survival1/*****Create a 1 by N -matrix of Survival ****/;

            end;

   

      print SurvivalM;

    InverseC=J(nrow(survival1),1,1);

          do i = 1 to n;

      if censored=1 then InverseC=1/survival1; else if censored[1]=0 then InverseC=0/*****survivalC is the imputed censored survival and set the real Survivalval to 1**/;

   

     *InverseCC=J(nrow(survival1),1,1);

end;

      print InverseC;

Big=SurvivalM*t(InverseC);

print BIG;

/*** for this code I want to delete all colums of BIG where row1 and col1 are zero ***/;

BIGG=J(nrow(BIGG),20,1);

do 1 to n;

if col[i,1]=0 then delete;

end;

print BIGG;

statement : ASSIGN at line 4338 column 1

4339  do 1 to n;

         -

         22

         200

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

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

4340  if col[i,1]=0 then delete;

4341  end;

ERROR: END does not occur within DO group at line=4341 col=1.

4342  print BIGG;

ERROR: Matrix BIGG has not been set to a value.

statement : PRINT at line 4342 column 1

Solution
‎02-02-2013 01:33 AM
Frequent Contributor
Posts: 122

Re: How to delete all columns where the col (x, 1)=0, that is the first number in the column is zero

Use something like:

reduced=BIG[, loc( BIG[ 1,]^= 0 )];

so use the result of the loc function to pick out the list of columns required.  The same technique can be used to remove zeros from the vector in one of your other questions.

Ian.

Super Contributor
Posts: 271

Re: How to delete all columns where the col (x, 1)=0, that is the first number in the column is zero

Thanks, it work

SAS Super FREQ
Posts: 3,232

Re: How to delete all columns where the col (x, 1)=0, that is the first number in the column is zero

Here is my advice: Your questions are all the same because you are working on a big problem. Do something easy like

u = 1:5;

v = {0,1,0,0,1};

Figure out how to do what you want with small vectors and matrices. After you understand the small simple case, the bigproblem will fall.

It will also make it easier for people on the discussion forum to answer your questions.  A lot of people just hit the DELETE button if there isn't a short, simple, cut-an-paste example that illustrates the question.

Post a Question
Discussion Stats
  • 4 replies
  • 294 views
  • 5 likes
  • 3 in conversation