DATA Step, Macro, Functions and more

two do loops in one macro

Accepted Solution Solved
Reply
Valued Guide
Posts: 860
Accepted Solution

two do loops in one macro

I am trying to generate two separate do loops within one macro.  Anytime I get it to go through it goes like an array which I do not want.  Without a macro the code looks like this:

data cus_hsd_&month1;

set cus_hsd_&month1;

format ver_start_day_key ver_end_day_key 8.;

ver_start_day_key= &lock1;

ver_end_day_key= &lock0;

run;

I have 18 of these which I'd like to do in one step,  I want the start date to be separate from the end date (the last two lines)  I'm not sure how to do that so I tried it with two do loops:

%macro ver_dates;

%do sd=1 %to 17;

  data cus_hsd_&&month&sd;

  set cus_hsd_&&month&sd;

  format ver_start_day_key ver_end_day_key 8.;

  ver_start_day_key = &&lock&sd;

%do ed=0 %to 16;

  ver_end_day_key = &&lock&ed;

%end;

  run;

%end;

%mend;

%ver_dates;

I've tried it many different ways, anytime it does work it's an array and the only date saved is the last date that ran.  The first do loop runs through with every step, the last do loop runs at the very end and the last date is saved for every end_day_key.

Any help or suggestions will be appreciated.  I'm not sure if I'm going about this the right way at all, just trying to neaten up some code in my spare time.

Thanks,


Accepted Solutions
Solution
‎07-19-2012 03:24 PM
Super Contributor
Posts: 1,636

Re: two do loops in one macro

Posted in reply to Steelers_In_DC

Hi,

try the modified code:

%macro ver_dates;
%do sd=1 %to 17;
  data cus_hsd_&&month&sd;
  set cus_hsd_&&month&sd;
  format ver_start_day_key ver_end_day_key 8.;
  ver_start_day_key = &&lock&sd;
  %let ed=%eval(&sd-1);
  ver_end_day_key = &&lock&ed;
  run;
%end;
%mend;

%ver_dates

Linlin

View solution in original post


All Replies
Solution
‎07-19-2012 03:24 PM
Super Contributor
Posts: 1,636

Re: two do loops in one macro

Posted in reply to Steelers_In_DC

Hi,

try the modified code:

%macro ver_dates;
%do sd=1 %to 17;
  data cus_hsd_&&month&sd;
  set cus_hsd_&&month&sd;
  format ver_start_day_key ver_end_day_key 8.;
  ver_start_day_key = &&lock&sd;
  %let ed=%eval(&sd-1);
  ver_end_day_key = &&lock&ed;
  run;
%end;
%mend;

%ver_dates

Linlin

Valued Guide
Posts: 860

Re: two do loops in one macro

Thanks so much.  That is exactly what I needed!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 261 views
  • 0 likes
  • 2 in conversation