Skip to content

Instantly share code, notes, and snippets.

@statgeek
statgeek / haversine_distance.sas
Created March 9, 2016 16:00
Calculate Haversine Distance in SAS using a function
/*This code is from SAS Communities question:
https://communities.sas.com/t5/Base-SAS-Programming/Reflexive-join-but-limit-some-of-the-joining/m-p/255561
*/
%let rad = constant('pi') / 180; /*degrees to radians*/
%let r_km = 6371; /*radius of the earth in km (3959 miles)*/
proc fcmp outlib = work.funcs.haversine;
function haversine(lat1, long1, lat2, long2); /*function name*/
/*function definition*/
@statgeek
statgeek / Chi Square Summary.sas
Created December 22, 2016 21:05
Calculate Chi Square for a summary table in SAS
/*
Author: F. Khurshed
Date: June 17, 2011
Purpose: This macro creates a set of chisquare tables appended together. It includes the chisquare value.
January 17, 2012
*Adding in the ability for the macro to add the total column
February 6, 2012
*Add in row of overall by cross category
@statgeek
statgeek / sas_time_series_lag
Created March 7, 2017 21:17
SAS Macro to create lagged time series data for variables
%macro lag(vars, lags);
%let m = %sysfunc(countw(&vars));
%do i=1 %to &m;
%let var = %scan(&vars,&i);
%do j=1 %to &lags;
%do;
lag_&var.&j = lag&j(&var);
%end;
@statgeek
statgeek / ps_text_file_utilities
Last active April 5, 2017 20:19
Windows Powershell - Extract top N lines from file to a new file
*Extract n lines to a new file;
gc -path file_name.csv - head N > output.txt
*Get the number of lines in a text file;
Get-Content "Path to file"| Measure-Object -Line
@statgeek
statgeek / sas_dynamically_format_labels
Created May 10, 2017 01:05
SAS - dynamically update labels to propcase
data class;
set sashelp.class;
label name="NAme" sex="GEndeR" age="Age" height="HeiGht" weight="kilograms";
run;
proc sql noprint;
select catx('=', name, quote(propcase(coalescec(trim(label), name))))
into :label_list separated by " " from sashelp.vcolumn where libname='WORK'
and upper(memname)='CLASS';
quit;
@statgeek
statgeek / sas_individual_words
Created May 30, 2017 17:34
SAS - Split file into individual words
data random_sentences;
infile cards truncover;
informat sentence $256.;
input sentence $256.;
cards;
This is a random #sentence
This is another #random sentence
Happy Birthday #test#together
My job#sucks.
This is a good idea, not.
@statgeek
statgeek / proc_sort_output_duplicates.sas
Created July 10, 2017 16:07
Identify and output duplicate records
/*This code demonstrates how to keep only duplicate observations in a data set*/
%*Create sample data set;
data have;
informat name $80.;
input name $ found;
cards;
If_True 1
If_True_kary 1
If_True_kary 1
@statgeek
statgeek / sas_timeSeries_procExpand.sas
Created July 11, 2017 20:28
SAS Time Series Data - Fill time series with missing time points and calculate moving average
/*this is an example of time series data.
1. Create a time series data set with missing intervals (IBM)
2. Add back missing entries using PROC TIMESERIES (IBM_NO_MISSING)
3. Calculate moving average - 12 month average
*/
/*1*/
data ibm;
set sashelp.stocks;
where stock='IBM';
@statgeek
statgeek / sas_split_data_number_records
Created August 27, 2017 18:24
SAS - split dataset into subsets based on number of records
*create a sample data set;
data have;
do i=1 to 7000;
x=rand('bernoulli', 0.4);
output;
end;
run;
*Set group size;
%let group_size = 1000;
@statgeek
statgeek / sas_random_number
Created August 27, 2017 18:30
SAS - create random numbers for each observation
/*How to create 50 random numbers for every observation in your data set */
data want;
set sashelp.class;
do i=1 to 50;
zv = rannor(0);
output;
end;