Skip to content

Instantly share code, notes, and snippets.

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;
*sort for faster processing and add index;
proc sort data=have out=have (index=(date));
by date;
statgeek /
Created Feb 16, 2022
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);
statgeek /
Created Dec 15, 2021
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;
*create format;
data counts_fmt;
statgeek /
Last active Nov 5, 2021
SAS - merge with formatted data
*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 /
Last active Oct 27, 2021
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 /
Created Sep 16, 2021
SAS - temporary array - word/term search with multiple words
/*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 (obs=5);
search_term = substr(team,1,3);
keep search_term;;
View r_tidyverse_center_variables.R
#This program centers all variables
#This is also called standardization - each variable minus the mean.
#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 /
Created Jun 25, 2021
SAS - dynamically generate a list for filtering
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 /
Created May 6, 2021
SAS - Conversion between types
data char_num;
char = "8.4"; output;
char = "10.5"; output;
data char_date;
char = "2012-01-01";output;
char = "2014-02-08"; output;
statgeek /
Last active Mar 25, 2021
SAS - ODS EXCEL multiple sheets macro solution
/*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