Skip to content

Instantly share code, notes, and snippets.

@statgeek
statgeek / SAS_ordinal_fmt
Created June 4, 2014 15:17
Create a format for ordinal numbers (1-1st, 2-2nd)
data ordinal_format;
length label $8.;
do number=1 to 300;
numstring = put(number, 16. -l);
if prxmatch('/(?<!1)1\s*$/',numstring) then ending='st';
else if prxmatch('/(?<!1)2\s*$/',numstring) then ending='nd';
else if prxmatch('/(?<!1)3\s*$/',numstring) then ending='rd';
else ending = 'th';
ordstring =catt(numstring, ending);
@statgeek
statgeek / SAS_input_multiple_txt
Last active May 8, 2019 15:01
SAS - read in multiple text files using wild card and get the filename
data import_all;
*make sure variables to store file name are long enough;
length filename txt_file_name $256;
*keep file name from record to record;
retain txt_file_name;
*Use wildcard in input;
infile "Path\*.txt" eov=eov filename=filename truncover;
@statgeek
statgeek / sas_Missing.sas
Last active October 10, 2017 15:12
SAS - Missing Macro - For a dataset, variable level report the number of missing and non-missing observations
%macro sum_missing(libname, dsetin, dsetout);
*Delete old dataset;
proc datasets nodetails nolist;
delete &dsetout;
quit;
*Upcase all macro variables to have consistency;
data _null_;
call symput ("libname", upcase("&libname."));
call symput ("dsetin", upcase("&dsetin."));
@statgeek
statgeek / AppendPDF.sas
Last active July 10, 2020 03:50
Using SAS to Append PDF files together
******************************************************************
This requires Adobe Acrobat Professional
List files to append in order
******************************************************************;
Data Files2Append;
length file $256.;
file="C:\Test\Sample_out.PDF"; output;
file="C:\Test\Sample_out 2.PDF"; output;
run;
@statgeek
statgeek / compare_datasets.sas
Last active November 30, 2017 17:29
SAS_compare_dataset_variables
%macro compare_data(base=, compare=);
proc sql noprint;
create table base_in as
select name
from sashelp.vcolumn
where libname=upper(scan("&base", 1, "."))
and memname=upper(scan("&base", 2, "."))
order by varnum;
quit;
@statgeek
statgeek / SAS_GTL_Title
Created December 6, 2014 03:50
SAS - Add by value title to GTL Template
*valid in SAS 9.3+, using the dynamic variable _byval_ that is automatically created by SAS;
proc template;
define statgraph scatter;
dynamic _x _y _byval_;
begingraph;
entrytitle "Scatter Plot of " _x " by " _y " for Sex = " _byval_;
layout overlay;
scatterplot x=_x y=_y;
@statgeek
statgeek / SAS_moving_stats.sas
Last active January 6, 2018 05:04
SAS - moving min/max using arrays
/*
How to calculate a moving min/max with a window of 4.
Resets by group ID
https://communities.sas.com/message/244232
Courtesy of PGStats
*/
data want;
array p{0:3} _temporary_;
set have; by object;
@statgeek
statgeek / sas_one_way_summary.sas
Last active March 9, 2022 22:28
SAS - One Way Summary Table
/*
Description: Creates a One-Way Freq table of variables including percent/count
Parameters:
dsetin - inputdataset
varlist - list of variables to be analyzed separated by spaces
dsetout - name of dataset to be created
Author: F.Khurshed
Date: November 2011
@statgeek
statgeek / SAS_split_filevar.sas
Last active June 2, 2021 20:21
Split a data set into multiple text files
/*
This program will spit a data set into multiple csv files each with a header row
and the data row that meets the condition of a group. If you want a record for each row use a unique identifier
as your var_split.
Note that this may not work if you have a very large data set that cannot be sorted or the number of variables are
over 150ish
*/
*name of the data set with the original data;
%let lib_name = sashelp;
@statgeek
statgeek / SAS_labels_from_dataset.sas
Last active April 11, 2024 20:30
SAS - Create variable labels for a dataset based on external data set
*Create label data set;
data label_data_set;
length name label $25.;
name="Sex"; label="Gender"; output;
name="height"; label="Height (in)"; output;
name="weight"; label="Weight (lbs)"; output;
run;
*Create sample dataset to apply label;