BookmarkSubscribeRSS Feed
tparvaiz
Obsidian | Level 7

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

3 REPLIES 3
Astounding
PROC Star

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.

tparvaiz
Obsidian | Level 7

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

Tom
Super User Tom
Super User

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?

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 743 views
  • 0 likes
  • 3 in conversation