Skip to content

Instantly share code, notes, and snippets.

@statgeek
statgeek / SAS_labels_from_dataset.sas
Last active April 11, 2024 20:30
SAS - Create variable labels for a dataset based on external data set
*Create label data set;
data label_data_set;
length name label $25.;
name="Sex"; label="Gender"; output;
name="height"; label="Height (in)"; output;
name="weight"; label="Weight (lbs)"; output;
run;
*Create sample dataset to apply label;
@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_categorize_variable
Last active October 31, 2023 17:02
SAS - How to categorize a continuous variable
data class;
set sashelp.class;
length category $20.;
bmi = 703*(weight/(height**2));
if bmi < 18 then
category='Under Weight';
else if 18 <= BMI < 25 then
category='Normal';
else if 25 <= BMI < 30 then
@statgeek
statgeek / sas_search_files_word.sas
Last active October 31, 2023 12:48
SAS - search text files for a single word
/*This code will search text files for a single word, search_string
Originally via @schmuel here:
https://communities.sas.com/t5/Base-SAS-Programming/Searching-SAS-code-for-keywords/m-p/390472#M93671
*/
%let search_string = rename;
%let suffix = sas;
%let root=/folders/myshortcuts/My_Folders/;
filename finp ("&root.sas_help/*.&suffix");
@statgeek
statgeek / SAS_combine_multiple_rows_into_one.sas
Created October 19, 2017 02:17
SAS - combine rows into one record with a delimiter
*create sample data for demonstration;
data have;
infile cards dlm='09'x;
input OrgID Product $ States $;
cards;
1 football DC
1 football VA
1 football MD
2 football CA
3 football NV
@statgeek
statgeek / Two Word Combinations.sas
Created December 20, 2017 16:12
SAS - take a sentence, split into individual words and find all two word combinations
*Create sample data;
data random_sentences;
infile cards truncover;
informat sentence $256.;
input sentence $256.;
cards;
This is a random sentence
This is another random sentence
Happy Birthday
My job sucks.
@statgeek
statgeek / SAS_time_process_stored.sas
Created January 9, 2019 18:14
SAS - track time processes
proc sql;
create table master_process_time
(Entry char(32),
StartTime num format=datetime22.4,
EndTime num format=datetime22.4,
duration num format=32.4,
recordTime num format=datetime22.4);
quit;
%global startTime endTime;
@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_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_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;