BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
Reeza
Super User

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;


 

abrown8_kumc_edu
Calcite | Level 5
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;


Reeza
Super User

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.

Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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