BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hellind
Quartz | Level 8

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

3 REPLIES 3
Reeza
Super User

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;

hellind
Quartz | Level 8

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.....

Reeza
Super User

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.;

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
  • 1520 views
  • 0 likes
  • 2 in conversation