Skip to content

Instantly share code, notes, and snippets.

@statgeek
statgeek / sas_wide_extrawide_double_transpose.sas
Created December 19, 2018 16:49
SAS - Wide to Extra Wide format - Double Transpose
/*This program shows an example of going from a wide format to an extra wide format.
Original question:
https://communities.sas.com/t5/SAS-Programming/Row-to-variable/m-p/522615/
*/
*Create sample data;
data have;
informat Case Variable $15.;
input Case $ Variable $ Y112 Y113 Y114;
cards;
@statgeek
statgeek / sas_split_number_records.sas
Created December 3, 2018 04:09
SAS - Split data set based on the number of records - fully dynamic
/*This macro splits a data set into data sets of size N.
The parameters requried are:
1. DSN = input data set name, such as sashelp.cars.
The libname should be included unless the data set
is in the work library.
2. Size = Number of records to be included in each data
set. Note that the last data set will be truncated,
ie if only 28 records are available only 28 will be
written to the output data set.
@statgeek
statgeek / SAS_rename_data_set_proc_datasets.sas
Last active August 11, 2020 15:46
SAS Rename - proc datasets
/*This code illustrates how to rename data sets dynamically*/
data _new_list;
set sashelp.vtable
(where=(upcase(libname)='WORK')) end=eof;
*filter list for only tables of interest;
*start proc datasets;
if _n_=1 then
@statgeek
statgeek / SAS_export_text_label_name.sas
Created November 21, 2018 18:24
SAS - export to CSV with labels and names
/*This is an example of how to export a data set with two header rows,
one that is labels and oen that is the variable names
*/
*Create demo data;
data class;
set sashelp.class;
label age='Age, Years' weight = 'Weight(lbs)' height='Height, inches';
run;
@statgeek
statgeek / SAS_table_summary_characteristics.sas
Created November 21, 2018 02:39
SAS - Table of Characteristics/Summary Table
/*
This macro creates a table of charateristics for the variables listed.
It handles categorical, binary and continuous variables and produces and output dataset that can be further customized. No statistical information is
included in this analysis
*/
/*Parameters to be set:
dsetin - name of dataset to be analyzed
cont = macro variable list of variable names, ie cont=age weight height
cat=list of categorical variables ie cat=sex grade
@statgeek
statgeek / SAS_example_informat.sas
Created November 15, 2018 01:46
SAS - Example of an informat/convert character to numeric
/*This is an exmaple of how to create an informat for a character variable.
It converts a character variable to a numeric one as well.
Author: F. Khurshed
Date: 2018-11-14
*/
*create format;
proc format;
invalue $ grade_fmt
@statgeek
statgeek / SAS_Christmas_Carol.sas
Created November 5, 2018 21:34
SAS Christmas Carol
/*This program plays a Christmas Carol
Not sure of original source but it was posted here:
https://groups.google.com/forum/#!topic/comp.soft-sys.sas/a9lmYmTNlv8
*/
data holidays;
retain fmtname '@$holiday';
length data $3;
@statgeek
statgeek / sas_proc_summary_maxid_minid.sas
Last active October 10, 2018 16:55
SAS - get a maximum and corresponding ID variable for the maximum value
/* Find a maximum value and the date that maximum value occurred
Using PROC SUMMARY to get the ID variables of statistics using MAXID and MINID statistics.
Tags: MinID, MaxID, PROC SUMMARY
*/
*create sample data;
data have;
input station $ datetime anydtdtm. calculatedpower ;
format datetime datetime.;
cards;
@statgeek
statgeek / SAS_find_nearest neighbour above value.sas
Created September 24, 2018 19:59
SAS - Find value in lookup table that is the minimum distance above your value
/*This finds the nearest value ABOVE a set value, it could be modified to find the lowest or nearest in general
Originally propsed here via F. Khurshed
https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value-to-a-List-of-Values/m-p/498539
*/
data t1;
input value1;
cards;
31
53
@statgeek
statgeek / Powershell - list all files in a folder and export to a CSV.ps1
Last active September 24, 2018 15:56
List all files in folders and export to a CSV file
<#
Description
A script for finding files in a directory and writing the list out to a CSV file
Modified from this one
#>
Write-Host "Enter Root Directory you would like to search"
Write-Host ""
Write-Host "Example: C:\Users\testuser"