IF statement with a macro - how to create a loop

Reply
Frequent Contributor
Posts: 134

IF statement with a macro - how to create a loop

Hi,

I have 10 if statements that uses a macro... is there a way to use a loop so that I don't have to type them 10 times

IF

     A_DATE  < = "&NEXT_DAY_1"dt+'07:00:00't  and

     A_DATE_END  < = "&NEXT_DAY_1"dt+'07:00:00't and

     B_DATE  >  "&NEXT_DAY_1"dt+'07:00:00't and

     B_DATE_END  >  "&NEXT_DAY_1"dt+'07:00:00't

THEN

     D2_VOL = VOL1;

ELSE D2_VOL = 0;

IF

     A_DATE  < = "&NEXT_DAY_2"dt+'07:00:00't  and

     A_DATE_END  < = "&NEXT_DAY_2"dt+'07:00:00't and

     B_DATE  >  "&NEXT_DAY_2"dt+'07:00:00't and

     B_DATE_END  >  "&NEXT_DAY_2"dt+'07:00:00't

THEN

     D2_VOL = VOL1;

ELSE D2_VOL = 0;

IF

     A_DATE  < = "&NEXT_DAY_3"dt+'07:00:00't  and

     A_DATE_END  < = "&NEXT_DAY_3"dt+'07:00:00't and

     B_DATE  >  "&NEXT_DAY_3"dt+'07:00:00't and

     B_DATE_END  >  "&NEXT_DAY_3"dt+'07:00:00't

THEN

     D3_VOL = VOL1;

ELSE D3_VOL = 0;

and so on

I am looking for something like this

do for X = 1 to 10

     IF

          A_DATE  < = "&NEXT_DAY_X"dt+'07:00:00't  and

          A_DATE_END  < = "&NEXT_DAY_X"dt+'07:00:00't and

          B_DATE  >  "&NEXT_DAY_X"dt+'07:00:00't and

          B_DATE_END  >  "&NEXT_DAY_X"dt+'07:00:00't

     THEN

          DX_VOL = VOL1;

     ELSE

          DX_VOL = 0;

Loop

Please advice

Thanks

Super User
Posts: 5,499

Re: IF statement with a macro - how to create a loop

tparvaiz,

The right tool for the job is arrays.  Here is an example that uses the macro variables you have already created.  All of this belongs in the DATA step:

array times {10} "&next_day_1"dt "&next_day_2"dt "&next_day_3"dt ... "&next_day_10"dt;

array vols {10} d1_vol d2_vol d3_vol ... d10_vol;

do _i_=1 to 10;

   vols{_i_}=0;

   if A_DATE < times{_i_} + '07:00:00't  and A_DATE_END <= times{_i_} + '07:00:00't

   and B_DATE > times{_i_} + '07:00:00't and B_DATE_END > times{_i_} + '07:00:00't

   then vols{_i_} = VOL1;

end;

You'll have to type out all 10 elements in each array.  But this is the right direction, I'm pretty sure.  See if it works for you.

Good luck.

Good luck.

Frequent Contributor
Posts: 134

Re: IF statement with a macro - how to create a loop

Posted in reply to Astounding

Hi Astounding,

my concatenate function is not working (As hilighted below)... if I can get only that working I think my problem will be solved...

DATA TABLE1;

SET TABLE1;

NEXT_DAY = 1;

Do date=today()to date=today()+14;

A_DATE  < = dhms(date+1,7,0,0)  and

A_DATE_END  < = dhms(date+1,7,0,0)  and

B_DATE  > dhms(date+1,7,0,0) and

B_DATE_END  >  dhms(date+1,7,0,0)

THEN

     'D'||Next_DAY||'_VOL' = VOL1;

ELSE 'D'||Next_DAY||'_VOL' = 0;

 

NEXT_DAY = NEXT_DAY+ +1;

End;

Thanks

Super User
Super User
Posts: 7,039

Re: IF statement with a macro - how to create a loop

That is not going to work.  You cannot create the name of the variable you want to reference in the code after the program has already been compiled.

Either use Array reference to determine at run time which variable to reference.

Or use an actual macro (not a macro variable) to generate a series of statements.

What is that actual problem that you want to do? 

What is the format of your input dataset?  What do you want to do with it.  How does the proposed structure that you are trying to create help?

Ask a Question
Discussion stats
  • 3 replies
  • 219 views
  • 0 likes
  • 3 in conversation