Skip to content

Instantly share code, notes, and snippets.

@statgeek
statgeek / SAS_create_format_other_freq.sas
Created December 15, 2021 01:55
Create an Other Category automatically within PROC FREQ
/*This is an example of how to create an Other category for everything except the top 3*/
*not ideal, other becomes first format - will look into how to make it last value;
*get counts;
proc freq data=sashelp.class order = freq;
table age / out=counts;
run;
*create format;
data counts_fmt;
@statgeek
statgeek / SAS_conversion_types.sas
Created May 6, 2021 23:08
SAS - Conversion between types
data char_num;
char = "8.4"; output;
char = "10.5"; output;
run;
data char_date;
char = "2012-01-01";output;
char = "2014-02-08"; output;
run;
@statgeek
statgeek / SAS_dictionary_columns_filter_selective.sas
Last active August 16, 2023 10:45
SAS - Dictionary Columns - how to filter a variable list to match pattern
*How to selectively filter your list of variables in a SAS data set with a pattern but not one that uses SAS variable shortcuts;
options mprint symbolgen;
%macro select(lib =, ds_in=, pattern=, ds_out=);
proc sql noprint;
select nliteral(name) into :var_list separated by ' '
from dictionary.columns
where libname = upcase("&lib")
@statgeek
statgeek / SAS_split_export.sas
Created February 16, 2022 16:22
SAS - export to SAS and split file by number of records or size of data set
*This macro will export a file to a data set and split it based on the number of records per sheet;
%macro export_split (dsn=, size=);
%*Get number of records and calculate the number of files needed;
data _null_;
set &dsn. nobs=_nobs;
call symputx('nrecs', _nobs);
n_files=ceil(_nobs/ &size.);
call symputx('nfiles', n_files);
stop;
run;
@statgeek
statgeek / SAS_stdRates.sas
Last active August 16, 2023 10:40
SAS - Standardized Rates
/*This program illustrates how to age standardize rates using the age data for Canada 1991*/
*Source Data:http://www.apheo.ca/resources/indicators/Standardization%20report_NamBains_FINALMarch16.pdf
*Source Code:http://support.sas.com/documentation/cdl/en/statug/65328/HTML/default/viewer.htm#statug_stdrate_syntax01.htm
;
*Create dataset with Canada Standard Rates for 1991;
data CanadaSTDRATE;
format AgeGroup 2. AgeGroupDesc $5. Canada1991 comma12.;
informat Canada1991 comma12.;
@statgeek
statgeek / SAS_macro_sankey_graph.sas
Last active May 12, 2023 16:33
SAS - Macro to create a Sankey diagram
/*
Author: Jeff Myers
Source: https://communities.sas.com/t5/Graphics-Programming/Sankey-Diagram-Decision-Tree-etc/m-p/719812#
*/
data random;
call streaminit(123);
do id = 1 to 100;
do cycle=1 to 5;
@statgeek
statgeek / SAS_7zip.sas
Last active April 4, 2023 17:37
SAS - Zip Files using 7Zip
options noxwait noxsync;
data _null_;
*Path to winzip program;
zipexe='"C:\Program Files\7-Zip\7z.exe" a -tzip';
*Zip file name;
zipfile="C:\My Documents\Sample.zip";
*File to be zipped;
file="C:\Temp\Sample.txt";
*Full cmd line to be passed to command line. It embeds the quotes for paths with spaces;
@statgeek
statgeek / sas_macro_convert_xlsx_csv.sas
Last active January 20, 2023 16:42
SAS - Macro to convert XLSX to CSV
/*This program will take a folder path (default) and look for files
that are specifically with the specified extension (ext) and convert
the files to CSV. It uses XCMD so you need to have that option enabled.
The original script is from here:
http://support.sas.com/kb/43/496.html
Modified to convert XLSX to CSV.
Author:F. Khurshed
Date: 2018-03-23
@statgeek
statgeek / SAS_fmtsearch_demonstration
Created October 7, 2022 20:03
Example of FMTSEARCH option usage in SAS
/*Author: F.Khurshed
Date: 2022-10-07*/
*create library to store format catalog;
libname demo '/home/fkhurshed/Demo1';
proc format;
value age_fmt
low - 12 = 'Child'
@statgeek
statgeek / SAS_macroLoopVars
Created March 17, 2014 16:49
SAS - Macro Loop Through Variable Lists
/*This Loops thoough a set of variables where the variables
are separated by "|". Any other delimiter can be used
and specified in the scan function as well*/
%macro loop(varlist);
%let i=1;
%do %while (%scan(&varlist, &i, |) ^=%str());
%let var=%scan(&varlist, &i, |);
%put &var;