DATA Step, Macro, Functions and more

Macros starting issue

Reply
Contributor
Posts: 67

Macros starting issue

im new on sas and finding it hard to use macros, i have following codes
copies from somewhere, they are run but do nothing, i think i have problem
referring to input data set. kindly help me with that thanks.

/*
***************************************************************************
******
*/
/* ******************** W R D S R E S E A R C H M A C R O S
******************** */
/*
***************************************************************************
******
*/
/* Summary : Compute Market-Model Betas */
/* Date : January, 2011 */
/* Author : Rabih Moussawi */
/* Variables : - S: Monthly/Daily, defaults to Monthly, but s=d for CRSP
Daily data */
/* - BEGDATE: Sample Start Date */
/* - ENDDATE: Sample End Date */
/* - WINDOW: Window of Estimation */
/* - MINWIN: Minimum Window of Estimation for non-missing betas */
/* - INDEX: Market Return Variable, with default Value-Weighted (VWRETD) */
/* - OUTSET: Output Dataset Name (default names crsp_m or crsp_d) */
/*
***************************************************************************
******
*/

%MACRO BETA
(S=m,START=01JAN2000,END=30JUN2001,WINDOW=36,MINWIN=12,INDEX=VWRETD,OUTSET=
beta_&s.);

/* Check Series: Daily or Monthly and define datasets - Default is Monthly
*/
%if &s=D %then %let s=d; %else %if &s ne d %then %let s=m;
%if (%sysfunc(libref(crsp))) %then %do;
 %let cs=/wrds/crsp/sasdata/;
 libname crsp ("&cs/m_stock","&cs/q_stock","&cs/a_stock");
%end;
%let sf = crsp.&s.sf ;
%let si = crsp.&s.si ;

options nonotes;
%put #### START. Computing Betas from &sf Using &WINDOW Estimation Window ;
data _crsp1 /view=_crsp1;
set &sf. ;
where "&START."D<=date<="&END."D;
keep permno date ret;
run;

proc sql;
create table _crsp2
as select a.*, b.&index, b.&index*(abs(a.ret)>=0) as X, a.ret*b.&index as
XY,
 (abs(a.ret*b.&index)>=0) as count
from _crsp1 as a left join &si. as b
on a.date=b.date
order by a.permno, a.date;
quit;

proc printto log = junk; run;
proc expand data=_crsp2 out=_crsp3 method=none;
by permno;
id date;
convert X=X2 / transformout= (MOVUSS &WINDOW.);
convert X=X / transformout= (MOVSUM &WINDOW.);
convert XY=XY / transformout= (MOVSUM &WINDOW.);
convert ret=Y / transformout= (MOVSUM &WINDOW.);
convert count=n / transformout= (MOVSUM &WINDOW.);
quit;
run;
proc printto; run;

data &outset;
set _crsp3;
if n>=&MINWIN. then beta=(XY-X*Y/n) / (X2-(X**2)/n);
label beta = "Stock Beta";
label n = "Number of Observations used to compute Beta";
drop X X2 XY Y COUNT;
format beta comma8.2 ret &index percentn8.2;
run;

/* House Cleaning */
proc sql;
drop view _crsp1;
drop table _crsp2, _crsp3;
quit;

options notes;
%put #### DONE . Dataset &outset. Created! ; %put ;

%MEND BETA;

/*
***************************************************************************
******
*/
/* ************* Material Copyright Wharton Research Data Services
*************** */
/* ****************************** All Rights Reserved
****************************** */
/*
***************************************************************************
******
*/

Regards

Super User
Posts: 19,780

Re: Macros starting issue

Posted in reply to raqthesolid

This is a macro definition. You call a macro after the definition by using the name and listing the parameters. This allows it to be reusable without repeating all the code. 

 

Heres a basic example. 

 

MACRO DEFINITION -> Print first 10 obs of a data set. 

%macro print_data(datain=);

 

proc print data=&datain (obs=10);

run;

 

%mend; 

 

MACRO EXECUTION -> Execute for 2 diff datasets

 

%print_data(sashelp.class);

%print_data(sashelp.air);

Super User
Super User
Posts: 7,942

Re: Macros starting issue

Posted in reply to raqthesolid

Hi,

 

"im new on sas and finding it hard to use macros" - in which case, start by thoroughly learning Base SAS which is the language you need to know.  Macro does nothing it is just a text generator.  If you don't understand what it is generating then you will have all kinds of problems.  There is nothing in that code you have given that cannot be done in a simple datastep, no need for pages of macro code.

Ask a Question
Discussion stats
  • 2 replies
  • 249 views
  • 2 likes
  • 3 in conversation