Hi!
I have a macro iteration loop where I make x number of columns in a table (the table is exported to Excel/csv)..
It works, but I want to have variable dates in the column headings.
Now the column names are:
Periode_1 | Periode_2 | Periode_x |
---|---|---|
Data | Data | Data |
I want them to be (example):
Periode: 01.01.2011 - 31.01.2011 | Periode: 01.02.2011-28.02.2011 | Periode:x - |
---|---|---|
Data | Data | Data |
I use the INTNX function in the If Then loop, and tried to use the same function (with some combinations) as a header name but no luck.
Any ideas? 🙂
--------------------------------------------------------------
My code looks something like this:
data TableOut;
set TableIn;
%macro Report(x, y);
%do Next=&x %to &y;
%let StartDate = '01MAR2011'd;
%let EndDate = '29FEB2012'd;
%let ColName = Periode_&Next;
if Value = 0
then &&ColName = 0;
else if (StartDate >= intnx('Month', &StartDate, -&Next, 'Same') and
StartDate <= intnx('Month', &EndDate, -&Next, 'Same') and
EndDate >= intnx('Month', &EndDate, -&Next, 'Same'))
then &&ColName = (Value/(EndDate-StartDate)) * (intnx('Month', &EndDate, -&Next, 'Same') - StartDate);
else if (StartDate <= intnx('Month', &StartDate, -&Next, 'Same') and
EndDate <= intnx('Month', &EndDate, -&Next, 'Same') and
EndDate >= intnx('Month', &StartDate, -&Next, 'Same'))
then &&ColName = (Value/(EndDate-StartDate)) * (EndDate - intnx('Month', &StartDate, -&Next, 'Same'));
else if (StartDate <= intnx('Month', &StartDate, -&Next, 'Same') and
EndDate >= intnx('Month', &EndDate, -&Next, 'Same'))
then &&ColName = (Value/(EndDate-StartDate)) * (intnx('Month', &EndDate, -&Next, 'Same') - intnx('Month', &StartDate, -&Next, 'Same'));
else if (StartDate >= intnx('Month', &StartDate, -&Next, 'Same') and
StartDate <= intnx('Month', &EndDate, -&Next, 'Same') and
EndDate >= intnx('Month', &StartDate, -&Next, 'Same') and
EndDate <= intnx('Month', &EndDate, -&Next, 'Same'))
then &&ColName = Value;
else &&ColName = 0;
%end;
%mend Report;
%Report(0,6);
run;
I want the ColName to be: Periode_01MAR2011-29FEB2012 insted of Periode_0
You do not really give enough data for me to fully understand your problem, so I made some assumptions:
data foo;
format periode_1 periode_2 periode_x date9.;
call streaminit(12345);
do i=1 to 10;
periode_1=today()-floor(rand('cauchy'));
periode_2=today()-floor(rand('cauchy'));
periode_x=today()-floor(rand('cauchy'));
output;
end;
drop i;
run;
proc print data=foo noobs; run;
periode_1 periode_2 periode_x
07MAR2012 09MAR2012 09MAR2012
08MAR2012 07MAR2012 08MAR2012
05MAR2012 09MAR2012 09MAR2012
09MAR2012 07MAR2012 08MAR2012
09MAR2012 04MAR2012 08MAR2012
09MAR2012 20FEB2012 07MAR2012
07MAR2012 09MAR2012 10MAR2012
05MAR2012 08MAR2012 17MAR2012
09MAR2012 08MAR2012 08MAR2012
07MAR2012 11MAR2012 08MAR2012
proc sql noprint;
select 'Periode: ' || put(min(periode_1),ddmmyyp10.) || ' - ' || put(min(periode_1),ddmmyyp10.),
'Periode: ' || put(min(periode_2),ddmmyyp10.) || ' - ' || put(min(periode_2),ddmmyyp10.),
'Periode: ' || put(min(periode_x),ddmmyyp10.) || ' - ' || put(min(periode_x),ddmmyyp10.)
into :label1,:label2,:labelx
from foo;
quit;
proc datasets lib=work memtype=data;
modify foo;
attrib periode_1 label="&label1"
periode_2 label="&label2"
periode_x label="&labelx";
run;
proc export
data=foo
dbms=xls
label
outfile='/path/to/output.xls'
replace;
run;
Periode: 05.03.2012 - 05.03.2012 | Periode: 20.02.2012 - 20.02.2012 | Periode: 07.03.2012 - 07.03.2012 |
7-Mar-12 | 9-Mar-12 | 9-Mar-12 |
8-Mar-12 | 7-Mar-12 | 8-Mar-12 |
5-Mar-12 | 9-Mar-12 | 9-Mar-12 |
9-Mar-12 | 7-Mar-12 | 8-Mar-12 |
9-Mar-12 | 4-Mar-12 | 8-Mar-12 |
9-Mar-12 | 20-Feb-12 | 7-Mar-12 |
7-Mar-12 | 9-Mar-12 | 10-Mar-12 |
5-Mar-12 | 8-Mar-12 | 17-Mar-12 |
9-Mar-12 | 8-Mar-12 | 8-Mar-12 |
7-Mar-12 | 11-Mar-12 | 8-Mar-12 |
You can add labels to these variables, then use proc export + label to export a xls file, the label of variable will instead of the name of variable.
Ksharp
To better understand what it is you are trying to do please provide a more accurate representation of what the input data from TableIn in your example code is like and the expected output.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.