DATA Step, Macro, Functions and more

Macro to set variable to recurring dates

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 92
Accepted Solution

Macro to set variable to recurring dates

Currently I do it manually, but I want to automate it.

This is how I currently set the variables to every Friday in YYMMDDn8. format.

%let week1=20150403;

%let week2=20150327;

%let week3=20150320;

%let week4=20150313;

%let week5=20150306;

%let week6=20150227;

%let week7=20150213;

%let week8=20150206

I have tried and this is what I come up with, but how to assign it to a variable?

data _null_;

do i = 0 to 54 by 7;

  dt=intnx('day','3apr2015'd,-i);

  call symput('week',d);

  put week=;

end;

run;


Accepted Solutions
Solution
‎04-09-2015 11:44 PM
Super User
Posts: 19,877

Re: Macro to set variable to recurring dates

That's sort of what I assumed. If you run my code above it creates the macro variables starting from week0 instead.

To increment to 1 instead, increment the j variable. The WANT dataset helps to visualize what's happening but isn't required.  Y

data want;

do i = 0 to 54 by 7;

  dt=intnx('day','3apr2015'd,-i);

j=floor(i/7)+1;

  call symput(catt('week', put(j, 2. -l)), put(dt, yymmddn8.));

  put dt=;

output;

end;

format dt yymmddn.;

run;



%put &week1.;

%put &week2.;

%put &week7.;

You could also use week  in your intnx function and loop your i for the number of weeks you're interested in. Also, I generally use Call symputX instead of Call symput as it trims spaces out automatically.

%let date_start=01Apr2015;

data _null_;

do i = 1 to 12;

  dt=intnx('week', "&date_start"d,-i);

  call symputX(catt('week_new', put(i, 2. -l)), put(dt, yymmddn8.));

  put dt=;

output;

end;

format dt yymmddn.;

run;

%put &week_new1.;

%put &week_new2.;

%put &week_new12.;

View solution in original post


All Replies
Super User
Posts: 19,877

Re: Macro to set variable to recurring dates

What do you mean by assign it to a variable? If you want a table don't use a data _null_ step.

Your week macro variable will only have the last value since you use the same macro variable name in each iteration of the loop.  Perhaps add the i to the variable name?

Also, week is a macro variable so it isn't available in the same data step. There are workarounds for this, but probably not what you're looking for.

Perhaps something like the following:

data want;

do i = 0 to 54 by 7;

  dt=intnx('day','3apr2015'd,-i);

j=floor(i/7);

  call symput(catt('week', put(j, 2. -l)),d);

  put week=;

output;

end;

run;

Frequent Contributor
Posts: 92

Re: Macro to set variable to recurring dates

Thanks for your assistance.

I don't want a table, but I want to achieve this:

%let week1=20150403;

%let week2=20150327;

%let week3=20150320;

%let week4=20150313;

%let week5=20150306;

%let week6=20150227;

%let week7=20150213;

%let week8=20150206

and so on.....

Solution
‎04-09-2015 11:44 PM
Super User
Posts: 19,877

Re: Macro to set variable to recurring dates

That's sort of what I assumed. If you run my code above it creates the macro variables starting from week0 instead.

To increment to 1 instead, increment the j variable. The WANT dataset helps to visualize what's happening but isn't required.  Y

data want;

do i = 0 to 54 by 7;

  dt=intnx('day','3apr2015'd,-i);

j=floor(i/7)+1;

  call symput(catt('week', put(j, 2. -l)), put(dt, yymmddn8.));

  put dt=;

output;

end;

format dt yymmddn.;

run;



%put &week1.;

%put &week2.;

%put &week7.;

You could also use week  in your intnx function and loop your i for the number of weeks you're interested in. Also, I generally use Call symputX instead of Call symput as it trims spaces out automatically.

%let date_start=01Apr2015;

data _null_;

do i = 1 to 12;

  dt=intnx('week', "&date_start"d,-i);

  call symputX(catt('week_new', put(i, 2. -l)), put(dt, yymmddn8.));

  put dt=;

output;

end;

format dt yymmddn.;

run;

%put &week_new1.;

%put &week_new2.;

%put &week_new12.;

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 201 views
  • 0 likes
  • 2 in conversation