Skip to content

Instantly share code, notes, and snippets.

@statgeek
statgeek / tablen.sas
Created May 10, 2024 15:11
TableN Macro - Author Jeff Meyers
/*------------------------------------------------------------------*
| MACRO NAME : tablen
| SHORT DESC : Creates a descriptives table of multiple variables
*------------------------------------------------------------------*
| CREATED BY : Meyers, Jeffrey (07/28/2016 3:00)
*------------------------------------------------------------------*
| VERSION UPDATES:
| 2.46 11/03/2021
| Changed code to avoid truncating character strings at 200
| 2.45 08/25/2021
@statgeek
statgeek / SAS_unzip_copy_extract_xpt.sas
Created November 10, 2023 21:28
SAS - Read a zipped xpt file into SAS
*path to the zip file;
filename src zip "/home/fkhurshed/Demo2/P_DR2IFF.zip";
*path to where to save the xpt file;
filename xl "/home/fkhurshed/Demo2/P_DR2IFF.xpt" ;
*extract file from zip - P_DR2IFF.XPT in the code below is the name of the file in the zipped file that is to be extracted;
data _null_;
/* using member syntax here */
infile src(P_DR2IFF.XPT)
@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_moving_stats_monthly
Created February 26, 2022 03:01
SAS - moving statistics 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 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_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_merge_groupformat.sas
Last active November 5, 2021 16:20
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
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_word_term_search.sas
Created September 16, 2021 14:28
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 sashelp.baseball (obs=5);
search_term = substr(team,1,3);
keep search_term;;
run;
@statgeek
statgeek / r_tidyverse_center_variables.R
Created July 23, 2021 16:17
R - Center all columns
#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,