Skip to content

Instantly share code, notes, and snippets.

@joosti
joosti / sas_macros_sql_into.sas
Last active August 29, 2015 14:12
SAS macros using SQL into
/* push number of unique firms into macro variable numfirms */
proc sql;
select count(*) into :numfirms from (select distinct gvkey from work.sample);
quit;
%put Number of distinct firms in sample: &numfirms;
@joosti
joosti / SAS_macro_if_then_else.sas
Last active August 12, 2024 20:14
SAS macros conditional code
/*
macro that computes the Herfindahl index for each industry-year
indVar can be SIC or NAICS
dsin needs to have gvkey, fyear, SICH or NAICSH
sample use: %computeIndSales(dsin=work.start, dsout=work.indsales, indVar=NAICS);
*/
%macro computeIndSales(dsin=, dsout=, indVar=SIC);
/* code for SIC */
@joosti
joosti / SAS_data_random_dates.sas
Last active August 29, 2015 14:12
SAS data step random dates
/* begin and end date to sample random game release dates */
%let startDate = 01/01/2006;
%let endDate = 12/31/2011;
/* convert to integer (internal representation of dates) */
%let startDateInt = %sysfunc(inputn(&startDate,MMDDYY10.));
%let endDateInt = %sysfunc(inputn(&endDate,MMDDYY10.));
/* range is 2190 days for 2006-2011 */
%let range = %eval(&endDateInt - &startDateInt);
@joosti
joosti / SAS_data_if_then.sas
Created December 27, 2014 14:58
SAS data step conditional code
data sample;
set sample;
if year <= 1997 then do;
preSFAS131 = 1;
/* .. more code .. */
end;
else do;
preSFAS131 = 0;
/* .. more code .. */
end;
@joosti
joosti / sas_macro_connectwrds.sas
Created December 27, 2014 01:45
SAS macro to connect with WRDS
%macro connectWrds;
%let wrds = wrds.wharton.upenn.edu 4016; options comamid = TCP remote=WRDS;
signon username=_prompt_;
%mend;
@joosti
joosti / sas_macro_variables.sas
Last active August 29, 2015 14:12
SAS macro variables
/* variable assignments */
%let tableDir = C:\myProject\sas_output\;
%let vars = roa roe asset_turn;
/* create descriptive statistics */
proc means data=myDataset NOPRINT;
output out=table1 mean= median= std= min= max= p25= p75= N=/autoname;
var &vars;
run;
@joosti
joosti / proc_datasets_libinfo.sas
Last active August 29, 2015 14:10
SAS proc datasets macros
/*
libInfo - info on datasets in library
libInfo (lib=, dset=, attr=, eng=, vars=)
lib: library, default is work
dset: dataset, default is _all_
attr: output dataset with attributes, default is work.attr
eng: output dataset with engine, default is work.eng
vars: output dataset with variables, default is work.vars
@joosti
joosti / proc_sql_self_join_1.sas
Created December 2, 2014 01:29
SAS Proc SQL Self join
proc sql;
create table work.dsOut as
/* select a.* means select all variables in a
select a.sale / b.sale - 1 means divide sales of year a by sales of year b minus 1
as sales_growth renames this to sales growth
b.fyear is included for debugging (to make sure prev_fyear is indeed previous year)
*/
select a.*, a.sale / b.sale - 1 as sales_growth, b.fyear as prev_fyear
from
work.dsIn a, work.dsIn b
@joosti
joosti / proc_sql_inner_join.sas
Created December 2, 2014 01:19
SAS Proc SQL Inner join
proc sql;
create table work.dsOut as
/* a.* means all records from work.dsIn
b.effdate means select effdate from misc.sdc
/
select a.*, b.effdate
from work.dsIn a
/* perform a left join */
left join misc.sdc b
/* require that gvkey is the same */
@joosti
joosti / sas_sql_groupby_1.sas
Last active August 29, 2015 14:10
SAS Proc SQL - Group by
/* count observations per firm: group by on one variable */
proc sql;
create table work.dsOut as select gvkey, count(*) as numYears from work.dsIn group by gvkey;
quit;