Skip to content

Instantly share code, notes, and snippets.

View iangow's full-sized avatar
🏠
Working from home

Ian Gow iangow

🏠
Working from home
View GitHub Profile
@iangow
iangow / mult_periods.sql
Created October 27, 2015 14:27
Code to identify PERMNOs with non-overlapping listing periods (there are none)
WITH windows AS (
SELECT permno, namedt, nameenddt,
CASE WHEN lag(nameenddt) OVER w + interval '3 days' > namedt THEN 0
ELSE 1 END AS new_period
FROM crsp.stocknames
WINDOW w AS (PARTITION BY permno ORDER BY namedt, nameenddt)),
window_nums AS (
SELECT *, sum(new_period) OVER w AS listing_period
WITH
sics AS (
SELECT DISTINCT gvkey, sic::integer
FROM comp.company),
obs AS (
SELECT gvkey, datadate, fyear, COALESCE(sich, sic) AS sic, fic
FROM comp.funda
INNER JOIN sics
@iangow
iangow / get_fortran.sh
Created March 2, 2016 19:42
Code to get Fortran for R
curl -O http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2
sudo tar fvxz gfortran-4.8.2-darwin13.tar.bz2 -C /
CREATE AGGREGATE array_aggcat (anyarray)
( sfunc = array_cat,
stype = anyarray,
initcond = '{}'
);
@iangow
iangow / install_texlive.sh
Last active May 2, 2016 15:25
Install texlive packages on remote computer on this one
ssh -C hackintosh '/opt/local/bin/port installed | grep texlive' | \
perl -ne '$_ =~ s/^\s+([^\s]*).*?$/\1/; print "$_";' | \
xargs sudo port install
@iangow
iangow / illustrate_returns.R
Created May 7, 2016 14:45
Code to illustrate calculation of returns in R
Sys.setenv(PGHOST="iangow.me", PGDATABASE="crsp")
devtools::source_url(paste0("https://raw.githubusercontent.com/iangow/",
"acct_data/master/code/getEventReturnsDaily.R"))
sql <- "
SELECT DISTINCT permno, comnam, gvkey, rdq
FROM crsp.stocknames AS a
INNER JOIN crsp.ccmxpf_linktable AS b
ON a.permno=b.lpermno
INNER JOIN comp.fundq
@iangow
iangow / median.sql
Created May 7, 2016 20:37
Function to add median aggregate to PostgreSQL (requires PL/R)
CREATE AGGREGATE public.median(double precision) (
SFUNC=public.plr_array_accum,
STYPE=float8[],
FINALFUNC=r_median
);
CREATE OR REPLACE FUNCTION public.r_median(double precision[])
RETURNS double precision AS
$BODY$
median(arg1)
@iangow
iangow / stata_files_used.pl
Last active May 21, 2016 14:47
Code to identify data files that Stata loads or saves
#!/usr/bin/env perl
# Get do file
@lines = <STDIN>;
# Get files used
foreach my $line (@lines) {
if ($line =~ /(?:use|(?:merge|joinby).*using)\s+"?(\w+)/) {
push @stata_files, $1;
}
}
@iangow
iangow / env_test.py
Created May 14, 2016 13:44
Code snippet to test environment variables in Python.
import os
print(os.environ['PGHOST'])
@iangow
iangow / upgrade_r_packages.R
Last active May 19, 2020 17:51
Snippet to reinstall R packages after an upgrade
pkgs <- list.files("/Library/Frameworks/R.framework/Versions/3.6/Resources/library")
install.packages(pkgs)