DATA Step, Macro, Functions and more

Macros starting issue

Posts: 58

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) */


/* 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");
%let sf = crsp.&s.sf ;
%let si = crsp.& ;

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;

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

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.);
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;

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

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


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


Super User
Posts: 17,912

Re: Macros starting issue

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);





MACRO EXECUTION -> Execute for 2 diff datasets




Super User
Super User
Posts: 7,417

Re: Macros starting issue



"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
  • 3 in conversation