Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can we use sas functions within a macro code

Status
Not open for further replies.

vuppalanchi

Programmer
Nov 6, 2007
5
US
Hello there,

I have been trying to use the sas inbuilt function MONTH to extract the month of a particular transaction date. And I need to write same set of steps for all the 12 months and therefore tried using macro. I am not sure how SAS recognizes functions within a macro. I have also tried using %sysfunc tool before the month function but its not working since I guess %sysfunc supports limited set of functions. Following is the code I was trying. Let me know if there any ways to handle this.

libname temp '/pool_mo_2007';
libname tempfile '/cii/phase4/work';

%macro transaction(outputdata,inputdata,monum,moname);
data tempfile.&outputdata;
set temp.&inputdata (keep=tdate);
informat tdate mmddyy6.;
%if %sysfunc(MONTH(tdate))=&monum %then current_month=1;
%else
%if %sysfunc(MONTH(tdate))=&monum-1 %then last_month=1;
%else other_months=1;

proc means data=tempfile.&outputdata N;
var current_month last_month other_months;
title 'Transaction Summary for the month of &moname';
run;
%mend;

 
I think you need to add a "run;" before your proc means statement to complete the data step you have before it.
 
Isn't the run statement optional as the data step is followed by a proc step. Even if I add the run statement its not working as the error is showing near the MONTH() function.
 
Can you explain a bit more on what you need to do as well as how your data is stored? I think that you are not using macro correctly here.

The reason your month function bombs is the way %sysfunc() works. It is executed at compile time. The TDATE variable has no value until run time.

That said, I believe you don't need macro for what you need to do. So tell us how your data is both stored and saved (file names etc) and what you need to do. I bet we can help you.

Klaz
 
The following program works for a single month.

data libname.outputdata;
set libname.inputdata (keep=tdate);
if month(tdate) in (01,02,03,04,05,06,07,08,09,10,11,12) then all_mon_trans=1;
else no_trans=1;
select(MONTH(tdate));
when(01)
current_mon_trans=1;
when(12)
dec_mon_trans_in_jan=1;
when(11)
nov_mon_trans_in_jan=1;
when(10)
oct_mon_trans_in_jan=1;
when(09)
sep_mon_trans_in_jan=1;
when(08)
aug_mon_trans_in_jan=1;
when(07)
jul_mon_trans_in_jan=1;
when(06)
jun_mon_trans_in_jan=1;
when(05)
may_mon_trans_in_jan=1;
when(04)
apr_mon_trans_in_jan=1;
when(03)
mar_mon_trans_in_jan=1;
when(02)
feb_mon_trans_in_jan=1;
otherwise;
end;
run;

proc means noprint data=tempfile.transdata N;
var all_mon_trans no_trans current_mon_trans dec_mon_trans_in_jan nov_mon_trans_in_jan oct_mon_trans_in_jan
sep_mon_trans_in_jan aug_mon_trans_in_jan jul_mon_trans_in_jan jun_mon_trans_in_jan may_mon_trans_in_jan
apr_mon_trans_in_jan mar_mon_trans_in_jan feb_mon_trans_in_jan;
title 'Transaction Summary for the month of January';
output out=tempfile.Jansummary(drop=_type_ _freq_) N=Number_of_Transactions No_Transactions Jan Dec Nov Oct Sep Aug Jul Jun M
ay Apr Mar Feb;
run;

Now, I want to perform the same set of functions for all the remaining 11 months. Here only the input dataset, ouput dataset and current month keep changing and rest of all are the same. I thought we can use a macro with these three variables as parameters.
 
First, wouldn't each record have at least a month value? (by definition a sas date has a month value between 1-12) If yes then the first logic step is not needed. I also notice that your variable names are month specific. So if you were to run this report in Feb would these variable names change too?

They don't need to. The beauty of SAS is that there are many ways that you could generate this report. I will try to go along the logic steps that you started with.
try this:

Code:
libname temp '/pool_mo_2007';
libname tempfile '/cii/phase4/work';

%macro transaction(outputdata,inputdata,monum);
 /*** YOU NEED TO PUT A TWO LEVEL SAS DATA NAME - LIBNAME.DSNAME FOR BOTH OUT AND INPUT DATA ***
  *** FOR MONUM ENTER A NUMBER BETWEEN 1-12                                                  ***/
 %let Mon3 = %sysfunc(putn(%sysfunc(inputn(&monum/01/1960,mmddyy10.)),monname3.));
 %let Mon9 = %sysfunc(putn(%sysfunc(inputn(&monum/01/1960,mmddyy10.)),monname9.));
data &outputdata;
    set &inputdata (keep=tdate);
if 0 lt month(tdate) le 12 then
   all_mon_trans = 1;
  else no_trans = 1;
select(MONTH(tdate));
    when(01)
           current_mon_trans=1;
    when(12)
           dec_mon_trans_in_.&Mon3=1;
    when(11)
           nov_mon_trans_in_.&Mon3=1;
    when(10)
           oct_mon_trans_in_.&Mon3=1;
    when(09)
           sep_mon_trans_in_.&Mon3=1;
    when(08)
           aug_mon_trans_in_.&Mon3=1;
    when(07)
           jul_mon_trans_in_.&Mon3=1;
    when(06)
           jun_mon_trans_in_.&Mon3=1;
    when(05)
           may_mon_trans_in_.&Mon3=1;
    when(04)
           apr_mon_trans_in_.&Mon3=1;
    when(03)
           mar_mon_trans_in_.&Mon3=1;
    when(02)
           feb_mon_trans_in_.&Mon3=1;
    otherwise;
end;
run;

proc means noprint data=tempfile.transdata N;
var all_mon_trans no_trans current_mon_trans dec_mon_trans_in_.&Mon3
    nov_mon_trans_in_.&Mon3 oct_mon_trans_in_.&Mon3 sep_mon_trans_in_.&Mon3
    aug_mon_trans_in_.&Mon3 jul_mon_trans_in_.&Mon3 jun_mon_trans_in_.&Mon3 
    may_mon_trans_in_.&Mon3 apr_mon_trans_in_.&Mon3 mar_mon_trans_in_.&Mon3 
    feb_mon_trans_in_.&Mon3;
title "Transaction Summary for the month of &Mon9";
output out = tempfile..&Mon3.summary(drop=_type_ _freq_) 
       N   = Number_of_Transactions No_Transactions Jan Dec Nov Oct Sep Aug Jul Jun May Apr Mar Feb;
run;
%mend transaction;

Now to run this all you need to do is envoke the macro.

Code:
 %transaction(test.indata,work.outdata,1);

Is this what you needed?
Klaz
 
Hi Klaz,

Thank you very much for you help on this one. The code worked wonders.
Just a few things I changed in the code you have provided is that I have removed . from .&mon3 as it was giving syntax errors and also we need not give the libref's in the macro initialization as we are mentioning it during invokation.
On the whole, your code was very helpful to me and I learnt so many new things as I just started programming.

Just for everybody's reference I am attaching the working code here.

libname temp '/pool_mo_2007';
libname tempfile '/cii/phase4/work';

%macro transaction(outputdata,inputdata,monum);
%let Mon3 = %sysfunc(putn(%sysfunc(inputn(&monum/01/1960,mmddyy10.)),monname3.));
%let Mon9 = %sysfunc(putn(%sysfunc(inputn(&monum/01/1960,mmddyy10.)),monname9.));
data &outputdata;
set &inputdata (keep=tdate);
if 0 lt month(tdate) le 12 then
all_mon_trans = 1;
else no_trans = 1;
select(MONTH(tdate));
when(01)
current_mon_trans=1;
when(12)
dec_mon_trans_in_&Mon3=1;
when(11)
nov_mon_trans_in_&Mon3=1;
when(10)
oct_mon_trans_in_&Mon3=1;
when(09)
sep_mon_trans_in_&Mon3=1;
when(08)
aug_mon_trans_in_&Mon3=1;
when(07)
jul_mon_trans_in_&Mon3=1;
when(06)
jun_mon_trans_in_&Mon3=1;
when(05)
may_mon_trans_in_&Mon3=1;
when(04)
apr_mon_trans_in_&Mon3=1;
when(03)
mar_mon_trans_in_&Mon3=1;
when(02)
feb_mon_trans_in_&Mon3=1;
otherwise;
end;
run;

proc means noprint data=&outputdata N;
var all_mon_trans no_trans current_mon_trans dec_mon_trans_in_&Mon3
nov_mon_trans_in_&Mon3 oct_mon_trans_in_&Mon3 sep_mon_trans_in_&Mon3
aug_mon_trans_in_&Mon3 jul_mon_trans_in_&Mon3 jun_mon_trans_in_&Mon3
may_mon_trans_in_&Mon3 apr_mon_trans_in_&Mon3 mar_mon_trans_in_&Mon3
feb_mon_trans_in_&Mon3;
title "Transaction Summary for the month of &Mon9";
output out = &Mon3.summary(drop=_type_ _freq_)
N = Number_of_Transactions No_Transactions Jan Dec Nov Oct Sep Aug Jul Jun May Apr Mar Feb;
run;

proc print data=&Mon3.summary;
run;

%mend transaction;

%transaction(tempfile.transdata0702,temp.poolnu0702,1);



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top