Skip to content

Instantly share code, notes, and snippets.

@statgeek
statgeek / SAS_moving_stats_monthly
Created Feb 26, 2022
SAS - moving statistics monthly
View SAS_moving_stats_monthly
/*This program illustrates how to loop through by calendar months and calculate a moving statistic*/
data have;
set sashelp.stocks;
run;
*sort for faster processing and add index;
proc sort data=have out=have (index=(date));
by date;
run;
@statgeek
statgeek / SAS_split_export.sas
Created Feb 16, 2022
SAS - export to SAS and split file by number of records or size of data set
View SAS_split_export.sas
*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_create_format_other_freq.sas
Created Dec 15, 2021
Create an Other Category automatically within PROC FREQ
View SAS_create_format_other_freq.sas
/*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_merge_groupformat.sas
Last active Nov 5, 2021
SAS - merge with formatted data
View SAS_merge_groupformat.sas
*This example demonstrates how you can merge data with a formatted variable and not have to create a new variable;
data stocks_A;
set sashelp.stocks;
where stock='IBM';
format date yymmn6.;
*keep only relevant variables for testing;
keep date open;
*rename to identify source;
@statgeek
statgeek / SAS_dictionary_columns_filter_selective.sas
Last active Oct 27, 2021
SAS - Dictionary Columns - how to filter a variable list to match pattern
View SAS_dictionary_columns_filter_selective.sas
*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_word_term_search.sas
Created Sep 16, 2021
SAS - temporary array - word/term search with multiple words
View SAS_word_term_search.sas
/*This is an example of how to search through a list of terms and see if a field contains any of the values*/
*Make fake data to show example;
*terms to search for;
data terms;
set sashelp.baseball (obs=5);
search_term = substr(team,1,3);
keep search_term;;
run;
View r_tidyverse_center_variables.R
#This program centers all variables
#This is also called standardization - each variable minus the mean.
library(tidyverse)
#generate fake data
df <- structure(list(day = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
@statgeek
statgeek / SAS_dynamically_generate_filter_list.sas
Created Jun 25, 2021
SAS - dynamically generate a list for filtering
View SAS_dynamically_generate_filter_list.sas
/*
One common issue with using SQL pass through is that you sometimes want to use data that is on your SAS server. Rather than pass this information to the server, to be used in a subquery or join, you can pass the values directly by dynamically generating the code and lookup lists
*/
%let age = 14;
data test;
set sashelp.class end = eof;;
where age = &age.;
@statgeek
statgeek / SAS_conversion_types.sas
Created May 6, 2021
SAS - Conversion between types
View SAS_conversion_types.sas
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_ODS_EXCEL_multiple_sheets_macro.sas
Last active Mar 25, 2021
SAS - ODS EXCEL multiple sheets macro solution
View SAS_ODS_EXCEL_multiple_sheets_macro.sas
/*This illustrates how to pipe a table and graph to ODS EXCEL
using macros.
1. Replace BY with WHERE statements
2. Add in ODS EXCEL options to name sheet
3. Wrap code in macro - note main ods excel statements are outside of the macro.
4. Create a list of origins to run this for
5. Call macro for each origin
*/