Skip to content

Instantly share code, notes, and snippets.

@statgeek
statgeek / sas_macro_variables_calculations.sas
Last active April 27, 2018 19:20
SAS - macro variables and calculations
/*This is an example of doing calculations with a macro variable.
In this case, all integers allow us to use %EVAL(). Otherwise you
would need to use %SYSEVALF()*/
%let HYR = 2018;
%Let Yr2 = %eval(&HYr. - 1);
%Let Yr5 = %eval(&HYr. - 4);
%put HYR: &hyr;
%put YR2: &yr2.;
@statgeek
statgeek / sas_correlation_heat_map.sas
Last active October 25, 2021 18:00
SAS - correlation matrix and heat map
/*This program is based on the question here:
https://communities.sas.com/t5/SAS-GRAPH-and-ODS-Graphics/Removing-one-of-the-legends-from-a-heatmap/m-p/456131
The purpose is to create a correlation heatmap from a correlation matrix.
Author: F. Khurshed
Date: 2018-04-20
*/
*calculate correlation matrix for the data;
ods output PearsonCorr=Corr_P;
@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_demo_format.sas
Last active July 2, 2019 15:59
SAS - Example of how a format works
/*This program demonstrates how a built in SAS format works.
It uses the WORDS format to display the age as thirteen rather than 13.
It also creates two additional variables, one is numeric and formatted,
the second is a character variable with the same value
Author: F.Khurshed
Date: 2018-03-23
*/
*Create a sample data set;
@statgeek
statgeek / SAS_freq_crosstab_table.sas
Created February 28, 2018 16:58
PROC FREQ - Cross Tab Table in a long format
/*This program is used when you want multiple m*n tables and you want it summarized. In this case the one variable, SEX is in the
KEEP statement, this will change depending on the variables in the code*/
ods output crosstabfreqs=summary;
proc freq data=sashelp.class;
table sex*(_all_);
run;
@statgeek
statgeek / SAS_survival_template_macros.sas
Created January 8, 2018 00:15
SAS Macro - Modify Survival Templates
%macro ProvideSurvivalMacros;
%global atriskopts bandopts censored censorstr classopts
graphopts groups insetopts legendopts ntitles stepopts tiplabel
tips titletext0 titletext1 titletext2 xoptions yoptions;
%let TitleText0 = METHOD " Survival Estimate";
%let TitleText1 = &titletext0 " for " STRATUMID;
%let TitleText2 = &titletext0 "s"; /* plural: Survival Estimates */
%let nTitles = 2;
@statgeek
statgeek / Two Word Combinations.sas
Created December 20, 2017 16:12
SAS - take a sentence, split into individual words and find all two word combinations
*Create sample data;
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
My job sucks.
@statgeek
statgeek / sas_vbs_xml_xlsx.sas
Created October 25, 2017 15:53
SAS - convert XML to XLSX
/*Sourced from here:
http://support.sas.com/kb/43/496.html
*/
options noxwait noxsync;
%macro convert_files(default=,ext=);
data _null_;
file "'&default\temp.vbs'";
@statgeek
statgeek / SAS_combine_multiple_rows_into_one.sas
Created October 19, 2017 02:17
SAS - combine rows into one record with a delimiter
*create sample data for demonstration;
data have;
infile cards dlm='09'x;
input OrgID Product $ States $;
cards;
1 football DC
1 football VA
1 football MD
2 football CA
3 football NV
@statgeek
statgeek / SAS_Create_an_anonymized_key.sas
Created October 6, 2017 03:03
SAS - Map IDs to a Random Number to mask IDs
/*This program demonstrates how to create a basic anonymized
key for a unique identifier. Ensure you set the value in CALL
STREAMINIT()/RANDOM_SEED macro variable to ensure you can
replicate the keys if needed*/
%let random_seed = 30;
*list of unique values;
proc sql;
create table unique_list as