Skip to content

Instantly share code, notes, and snippets.

@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,
@statgeek
statgeek / SAS_dynamically_generate_filter_list.sas
Created June 25, 2021 17:44
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
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_ODS_EXCEL_multiple_sheets_macro.sas
Last active March 25, 2021 20:59
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
*/
@statgeek
statgeek / SAS_ODS_EXCEL_split_bygroup.sas
Created March 23, 2021 18:54
SAS - ODS Example using BY group to split data to separate sheets
/*This is an example of exporting data to excel, with each section going to a
separate sheet and each sheet taking the name of the group*/
*sorts your data to use BY logic;
proc sort data=sashelp.cars out=cars;
by origin;
run;
*removes proc title and by line which are usually printed by default;
ods noptitle;
@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_ods_control_output.sas
Created February 8, 2021 22:27
SAS - pipe output to different destinations
/*This is an example of how to control which output goes to which file*/
ods html (ID="dest2") file='/home/fkhurshed/Demo1/testODS1.html';
ods html (ID="dest3") file='/home/fkhurshed/Demo1/testODS2.html';
ods html (ID="dest2") select BasicMeasures;
ods html (ID="dest3") select Quantiles;
proc univariate data=sashelp.class;
run;
@statgeek
statgeek / SAS_macro_variable_space_comma.sas
Created January 19, 2021 00:42
SAS - convert macro variable space delimited list to comma delimited
*An example/demo of how to convert a macro variable list that is space delimited to comma delimited.
%Let weightvar = WEIGHT HEIGHT AGE;
*uses the translate function to replace spaces with commas. If VALIDVARNAME=ANY this may not work;
%macro addCommas(varList=);
%sysfunc(translate(&varList, %str(, ), %str( )))
%mend addCommas;
*example recoding;