turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- Conditionally Replacing Missing Values with 0

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted
# Conditionally Replacing Missing Values with 0

Options

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-09-2018 04:05 PM

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;

Accepted Solutions

Solution

02-09-2018
05:01 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to abrown8_kumc_edu

02-09-2018 04:35 PM

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.

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to abrown8_kumc_edu

02-09-2018 04:14 PM

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 =

0THEN 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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

02-09-2018 04:23 PM

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;

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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to abrown8_kumc_edu

02-09-2018 04:28 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to abrown8_kumc_edu

02-09-2018 04:35 PM

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.