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
- /
- IF statement with a macro - how to create a loop

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

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

07-14-2012 12:56 AM

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

D**X**_VOL = VOL1;

ELSE

D**X**_VOL = 0;

Loop

Please advice

Thanks

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

07-16-2012 05:14 PM

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.

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

07-16-2012 06:41 PM

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

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

07-16-2012 06:47 PM

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?