SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
lca
Calcite | Level 5 lca
Calcite | Level 5

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_1Periode_2Periode_x
DataDataData

I want them to be (example): 

Periode: 01.01.2011 - 31.01.2011Periode: 01.02.2011-28.02.2011Periode:x -
DataDataData

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

3 REPLIES 3
FriedEgg
SAS Employee

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.2012Periode: 20.02.2012 - 20.02.2012Periode: 07.03.2012 - 07.03.2012
7-Mar-129-Mar-129-Mar-12
8-Mar-127-Mar-128-Mar-12
5-Mar-129-Mar-129-Mar-12
9-Mar-127-Mar-128-Mar-12
9-Mar-124-Mar-128-Mar-12
9-Mar-1220-Feb-127-Mar-12
7-Mar-129-Mar-1210-Mar-12
5-Mar-128-Mar-1217-Mar-12
9-Mar-128-Mar-128-Mar-12
7-Mar-1211-Mar-128-Mar-12
Ksharp
Super User

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

FriedEgg
SAS Employee

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.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 1522 views
  • 0 likes
  • 3 in conversation