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;
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.
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;
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.