Recode in new variable

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

Recode in new variable

I am trying to Recode  'Cause of Death' (DEATHCAUSE) in two group Cause=1 (Heart Disease) and Cause= 0 (Any other cause). Can anyone point what is wrong with my code?

I got this error with my code:  ERROR 180-322: Statement is not valid or it is used out of proper order.

IF ('I00'<=DEATHCAUSE<='I09') OR                                                                                                      

(DEATHCAUSE EQ 'I11') OR                                                                                                                 

(DEATHCAUSE EQ 'I13') OR                                                                                                                 

('I20'<=DEATHCAUSE<='I51') THEN CAUSE=1;                                                                                             

ELSE CAUSE=0;                                                                                                                                     

RUN;


Accepted Solutions
Solution
‎12-17-2014 05:41 PM
Esteemed Advisor
Posts: 6,905

Re: Recode in new variable

Your code is misplaced. However, while I'm not familiar with ICD codes, if they always start with a letter, and there's no difference between the number of leading zeros, then you might want to use something like:

data have;

  input deathcause $;

  cards;

I01

I02

I10

I13

I14

I49

A002

X001

;

data want (drop=type num);

  set have;

  type=substr(strip(deathcause),1,1);

  num=input(substr(deathcause,anydigit(deathcause)),8.);

  if type eq 'I' and

  (

   (0<=num<=9) OR                                                                                                     

   (num EQ 11) OR                                                                                                                

   (num EQ 13) OR                                                                                                                

   (20<=num<=51)

  ) THEN CAUSE=1;

  ELSE CAUSE=0;                                                                                                                                    

run;

My examples only produce two variables because it initially only had one and added cause.

View solution in original post


All Replies
Grand Advisor
Posts: 9,748

Re: Recode in new variable

You should post the entire program LOG if practical. Often the cause of this particular error can be traced to an unclosed quote, ( or a missing ; in a line above.

Also the LOG usually shows an underscore character under the first position that the error is detected and that is helpful as well.

You need to be very careful with constructs like

('I00'<=DEATHCAUSE<='I09')

with character variables as 'I000' will usually come up as less than 'I09'.

Contributor
Posts: 21

Re: Recode in new variable

Thanks Ballardw,

Statement looks OK. I ran code sent by Arthur on it and it worked.

libname bwmc '\\xxxx\xxxxx\Death Data\SAS Datasets';

data newD13;

set bwmc.D13Copy;

run;

IF ('I00'<=DEATHCAUSE<='I09') OR                                                                                                    

(DEATHCAUSE EQ 'I11') OR                                                                                                               

(DEATHCAUSE EQ 'I13') OR                                                                                                               

('I20'<=DEATHCAUSE<='I51') THEN CAUSE=1;                                                                                           

ELSE CAUSE=0;                                                                                                                                   

RUN;

Grand Advisor
Posts: 9,748

Re: Recode in new variable

bikash wrote:

Thanks Ballardw,

Statement looks OK. I ran code sent by Arthur on it and it worked.

libname bwmc '\\xxxx\xxxxx\Death Data\SAS Datasets';

data newD13;

set bwmc.D13Copy;

run;

Having this statement after RUN will cause the out or order error as RUN ends the data step and IF does not start another executable such as DATA, PROC, %macro or options.

IF ('I00'<=DEATHCAUSE<='I09') OR                                                                                                   

(DEATHCAUSE EQ 'I11') OR                                                                                                              

(DEATHCAUSE EQ 'I13') OR                                                                                                              

('I20'<=DEATHCAUSE<='I51') THEN CAUSE=1;                                                                                          

ELSE CAUSE=0;                                                                                                                                  

RUN;

Esteemed Advisor
Posts: 6,905

Re: Recode in new variable

If your statements are within a datasetp they should work.  e.g.:

data have;

  input deathcause $;

  IF ('I00'<=DEATHCAUSE<='I09') OR                                                                                                     

   (DEATHCAUSE EQ 'I11') OR                                                                                                                

   (DEATHCAUSE EQ 'I13') OR                                                                                                                

   ('I20'<=DEATHCAUSE<='I51') THEN CAUSE=1;                                                                                            

  ELSE CAUSE=0;                                                                                                                                    

  cards;

I02

I10

I13

I14

I49

;

Contributor
Posts: 21

Re: Recode in new variable

Thanks Arthur,

I ran your code and got a new table with only two variables ; selected deathcause only (cards) and cause.

Deathcause Cause

I01                    1

i02                    1

i03                    1

I am trying to add variable in existing table which I want to look like this:

Deathcause Cause

I01                  1

I02                  1

A002                0

X001                0

Deathcause is ICD 10 mortality code which is alphanumeric with 3 or 4 character e.g. A01, A001 etc.

Solution
‎12-17-2014 05:41 PM
Esteemed Advisor
Posts: 6,905

Re: Recode in new variable

Your code is misplaced. However, while I'm not familiar with ICD codes, if they always start with a letter, and there's no difference between the number of leading zeros, then you might want to use something like:

data have;

  input deathcause $;

  cards;

I01

I02

I10

I13

I14

I49

A002

X001

;

data want (drop=type num);

  set have;

  type=substr(strip(deathcause),1,1);

  num=input(substr(deathcause,anydigit(deathcause)),8.);

  if type eq 'I' and

  (

   (0<=num<=9) OR                                                                                                     

   (num EQ 11) OR                                                                                                                

   (num EQ 13) OR                                                                                                                

   (20<=num<=51)

  ) THEN CAUSE=1;

  ELSE CAUSE=0;                                                                                                                                    

run;

My examples only produce two variables because it initially only had one and added cause.

Contributor
Posts: 21

Re: Recode in new variable

Thanks Arthur and Ballardw,

My colleague came up with this code using substr and it worked:

data newd13;

set bwmc.d13copy;

if substr(deathcause,1,2) in ('I0' 'I2' 'I3' 'I4' 'I5') then cause = 1;

else

if substr(deathcause,1,3) in ('I11' 'I13') then cause = 1;

else

  cause = 0;

run;

Post a Question
Discussion Stats
  • 7 replies
  • 3171 views
  • 0 likes
  • 3 in conversation