Skip to content

Instantly share code, notes, and snippets.

@statgeek
statgeek / SAS_drop_variables_missing.sas
Created January 18, 2021 23:28
SAS - drop variables with a percentage missing
%macro drop_missing_pct(input_dsn = , output_dsn=, pct = , id_vars=);
*input_dsn = input data set name;
*output_dsn = output data set name;
*pct = missing percent, variables with a percentage of missing above this value are dropped;
*id_vars = space delimited list of variables that you do not want to include in the analysis such as ID variables;
@statgeek
statgeek / sas_array_indicator_list.sas
Last active August 16, 2023 10:49
SAS - Arrays - Finding Name from Indicator List
*https://communities.sas.com/t5/SAS-Programming/combining-multiple-columns/m-p/705769
/*This example illustrates how to combine variables from labels or variable names
from a list of indicator variables
Author: F. Khurshed
Date: 2020-12-14
*/
data have;
@statgeek
statgeek / SAS_transpose_to_create_dummy_variables.sas
Last active December 9, 2020 20:33
SAS - Create Dummy or Indicator variables for codes
/*Modified from this question on SO: https://stackoverflow.com/questions/65219061/sas-loop-create-columns-from-the-records-which-are-having-a-value
Initial answer from Richard D.
*/
*sample data;
data have;
input id found code $;
datalines;
1 1 001
2 0 v58
@statgeek
statgeek / SAS_export_files_library.sas
Created September 25, 2020 18:20
SAS - export all files in a library
/*This program exports all files from a library into CSV files.
Author: F. Khurshed
Date: 2020-09-25
*/
options dlcreatedir;
proc options option=dlcreatedir;
run;
@statgeek
statgeek / sas_ensure_table_complete_categories.sas
Created June 25, 2020 16:16
SAS - Ensure table has all categories
/*This example goes over how to create a data set with all values. In this case item_id should have values 1 through 4 for every record
Missing values are included for the missing resp
Question originally from here: https://communities.sas.com/t5/SAS-Programming/How-to-format-this-data/m-p/665054
*/
*fake data;
data have;
input item_id $ resp;
datalines;
@statgeek
statgeek / r_month_number_to_name.R
Created June 5, 2020 16:15
R - convert a month number to a month name
month <- c(12,3,6,2,3,7)
month.abb[month]
#[1] "Dec" "Mar" "Jun" "Feb" "Mar" "Jul"
#To apply as label in graphs:
factor(month.abb[serviceMonth], levels=month.abb)
@statgeek
statgeek / r_save_file_with_date_stamp.R
Created May 13, 2020 19:16
R - Save file with datestamp
# This program illustrates how to save an RDS file with the date in the name
#file name
myfileName <- "exampleData"
#create full file name including date and extension
myfilePath <- paste0(getwd(), "/", myfileName, "_" ,Sys.Date(), ".RDS")
#pass variable to save function
saveRDS(mpg, myfilePath)
@statgeek
statgeek / r_ggplot_customize_order_categories.R
Last active May 11, 2020 19:19
R - ggplot - customize the order of categories on a chart
#This shows how to :
#1. customize the order of bars in a bar plot
#2. format legend
#3. Add labels to axis, graph title, legend
#4. Create clustered bar chart
#F.Khurshed
#2020-05-11
#create fake data to play with
demo <- dput(structure(list(days_stay = c("<=30", "<=30", "120+", "31 to 60",
@statgeek
statgeek / SAS_preloadfmt_tabulate.sas
Created September 21, 2019 18:57
SAS - Preloadfmt - reporting values not in data
/*This demonstrates how to ensure all levels are in your report
using the preloadfmt option*/
data raw;
input person firsttest $ secondtest $;
datalines;
1 Good Good
2 Poor Good
3 Good Bad
@statgeek
statgeek / SAS_rank_top_N.sas
Created September 21, 2019 18:42
SAS - Add ranks - Select Top N records
*Create ranks for variables value;
proc rank data=sashelp.class out=class_ranked
/* (where= (rank_weight < 4)) */
;
var weight;
ranks rank_weight;
run;
*display output;
title 'Unsorted but ranked';