Pyrite | Level 9

## 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

1 ACCEPTED SOLUTION

Accepted Solutions
Barite | Level 11

## 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.

4 REPLIES 4
Pyrite | Level 9

## 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

 ROW1 ROW2 ROW3 ROW4 ROW5 ROW6 ROW7 ROW8 ROW9 ROW10 ROW11 ROW12 ROW13 ROW14 ROW15 ROW16 ROW17 ROW18 ROW19 ROW20 0 0 1.08754 0 0 0 0 0 0 0 0 0 0 0 0 4.10159 0 0 0 0 0 0 1.0303 0 0 0 0 0 0 0 0 0 0 0 0 3.88571 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 3.77143 0 0 0 0 0 0 0.969697 0 0 0 0 0 0 0 0 0 0 0 0 3.65714 0 0 0 0 0 0 0.909091 0 0 0 0 0 0 0 0 0 0 0 0 3.42857 0 0 0 0 0 0 0.848485 0 0 0 0 0 0 0 0 0 0 0 0 3.2 0 0 0 0 0 0 0.787879 0 0 0 0 0 0 0 0 0 0 0 0 2.97143 0 0 0 0 0 0 0.727273 0 0 0 0 0 0 0 0 0 0 0 0 2.74286 0 0 0 0 0 0 0.666667 0 0 0 0 0 0 0 0 0 0 0 0 2.51429 0 0 0 0 0 0 0.606061 0 0 0 0 0 0 0 0 0 0 0 0 2.28571 0 0 0 0 0 0 0.545454 0 0 0 0 0 0 0 0 0 0 0 0 2.05714 0 0 0 0 0 0 0.484849 0 0 0 0 0 0 0 0 0 0 0 0 1.82857 0 0 0 0 0 0 0.424242 0 0 0 0 0 0 0 0 0 0 0 0 1.6 0 0 0 0 0 0 0.363636 0 0 0 0 0 0 0 0 0 0 0 0 1.37143 0 0 0 0 0 0 0.30303 0 0 0 0 0 0 0 0 0 0 0 0 1.14286 0 0 0 0 0 0 0.265151 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0.227273 0 0 0 0 0 0 0 0 0 0 0 0 0.857143 0 0 0 0 0 0 0.151515 0 0 0 0 0 0 0 0 0 0 0 0 0.571429 0 0 0 0 0 0 0.0757576 0 0 0 0 0 0 0 0 0 0 0 0 0.285714 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

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

Barite | Level 11

## 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.

Pyrite | Level 9

Thanks, it work

SAS Super FREQ

## 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.

From The DO Loop