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;
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.;
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;
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.....
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.;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.