Conditionally Replacing Missing Values with 0

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Conditionally Replacing Missing Values with 0

I have 30 variables that were 0/1 questions.  If the subject answered 0, the values for the subsequent 14 questions were missing.  I need to change the missing values of these 14 fields to 0 when the source field is 0.  All the subsequent 14 variables have the same suffix as the source field (and the suffixes start with 4 to 33).

 

For example, for the first one: 

   

         IF DaySmoke_Quest4 = 0 THEN DO;

                      Cigarett_Amount4 = 0;

                      NicGum_Amount4 = 0;

                      NicLoz_Amount4 = 0;

                      LitCigars_Amount4 = 0;

                      Cigarillos_Amount4 = 0;

                      NicPatch_Amount4 = 0;

                      Hookahs_Amount4 = 0;

                      Snuff_Amount4 = 0;

                      Bidis_Amount4 = 0;

                      Pipes_Amount4 = 0;

                      Blunts_Amount4 = 0;

                      FullCigars_Amount4 = 0;

                      ECig_Amount4 = 0;

                      HandCigs_Amount4 = 0;

                 END;

Attachment

Accepted Solutions
Solution
‎02-09-2018 05:01 PM
Super User
Posts: 6,624

Re: Conditionally Replacing Missing Values with 0

Posted in reply to abrown8_kumc_edu

Looking at the naming patterns of your variables, I would suggest macro language rather than arrays.  (You can add arrays as well, but it's harder and takes more SAS skills to both write and read the program.)  For example, the macro could look like this:

 

%macro if_then;

   %local k;

   %do k=4 %to 33;

         IF DaySmoke_Quest&k = 0 THEN DO;

                      Cigarett_Amount&k = 0;

                      NicGum_Amount&k = 0;

                      NicLoz_Amount&k = 0;

                      LitCigars_Amount&k = 0;

                      Cigarillos_Amount&k = 0;

                      NicPatch_Amount&k = 0;

                      Hookahs_Amount&k = 0;

                      Snuff_Amount&k = 0;

                      Bidis_Amount&k = 0;

                      Pipes_Amount&k = 0;

                      Blunts_Amount&k = 0;

                      FullCigars_Amount&k = 0;

                      ECig_Amount&k = 0;

                      HandCigs_Amount&k = 0;

                 END;

   %end;

%mend if_then;

 

With the macro written, you can add within a DATA step:

 

%if_then

 

That will generate the 420 assignment statements.

View solution in original post


All Replies
Super User
Posts: 23,224

Re: Conditionally Replacing Missing Values with 0

Posted in reply to abrown8_kumc_edu

Have you looked into arrays instead?

If you list the variables in an array and say item 6 is 0 you can easily set the next 14 values to 0 using a loop. 

 


abrown8_kumc_edu wrote:

I have 30 variables that were 0/1 questions.  If the subject answered 0, the values for the subsequent 14 questions were missing.  I need to change the missing values of these 14 fields to 0 when the source field is 0.  All the subsequent 14 variables have the same suffix as the source field (and the suffixes start with 4 to 33).

 

For example, for the first one: 

   

         IF DaySmoke_Quest4 = 0 THEN DO;

                      Cigarett_Amount4 = 0;

                      NicGum_Amount4 = 0;

                      NicLoz_Amount4 = 0;

                      LitCigars_Amount4 = 0;

                      Cigarillos_Amount4 = 0;

                      NicPatch_Amount4 = 0;

                      Hookahs_Amount4 = 0;

                      Snuff_Amount4 = 0;

                      Bidis_Amount4 = 0;

                      Pipes_Amount4 = 0;

                      Blunts_Amount4 = 0;

                      FullCigars_Amount4 = 0;

                      ECig_Amount4 = 0;

                      HandCigs_Amount4 = 0;

                 END;


 

New Contributor
Posts: 2

Re: Conditionally Replacing Missing Values with 0

Yes, I have tried with the array, but I am getting stuck on how to make the section after the DO into an array...



This is what I had:
ARRAY yesno {*} DaySmoke_Quest4--DaySmoke_Quest33;

DO i=4 to DIM(yesno);
%LET N=I;

IF yesno{i} = 0 THEN DO;

...

END;


Super User
Posts: 23,224

Re: Conditionally Replacing Missing Values with 0

Posted in reply to abrown8_kumc_edu

ARRAY yesno {*} DaySmoke_Quest4--DaySmoke_Quest33;

DO i=4 to DIM(yesno);


IF yesno{i} = 0 THEN DO;
    do j=i to i+14;

             yesno(j) = .;

     end;



END;

 

I'm not quite following partly because I have no data but see above for how you could code it. Note that you need to use a different index for this loop.

Solution
‎02-09-2018 05:01 PM
Super User
Posts: 6,624

Re: Conditionally Replacing Missing Values with 0

Posted in reply to abrown8_kumc_edu

Looking at the naming patterns of your variables, I would suggest macro language rather than arrays.  (You can add arrays as well, but it's harder and takes more SAS skills to both write and read the program.)  For example, the macro could look like this:

 

%macro if_then;

   %local k;

   %do k=4 %to 33;

         IF DaySmoke_Quest&k = 0 THEN DO;

                      Cigarett_Amount&k = 0;

                      NicGum_Amount&k = 0;

                      NicLoz_Amount&k = 0;

                      LitCigars_Amount&k = 0;

                      Cigarillos_Amount&k = 0;

                      NicPatch_Amount&k = 0;

                      Hookahs_Amount&k = 0;

                      Snuff_Amount&k = 0;

                      Bidis_Amount&k = 0;

                      Pipes_Amount&k = 0;

                      Blunts_Amount&k = 0;

                      FullCigars_Amount&k = 0;

                      ECig_Amount&k = 0;

                      HandCigs_Amount&k = 0;

                 END;

   %end;

%mend if_then;

 

With the macro written, you can add within a DATA step:

 

%if_then

 

That will generate the 420 assignment statements.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 180 views
  • 0 likes
  • 3 in conversation