Skip to content

Instantly share code, notes, and snippets.

@statgeek
statgeek / sas_moving_averages_proc_means.sas
Last active November 19, 2017 02:03
SAS macro moving averages proc means
proc sql noprint;
select min(period) into :min_period TRIMMED
from have;
select max(period) into :max_period TRIMMED
from have;
quit;
%put &min_period;
%put &max_period;
@statgeek
statgeek / sas_proc_sort_duplicate_keys
Created September 2, 2017 19:55
SAS - PROC SORT identifying duplicate records
/*This example demonstrates how to get a list of duplicates based on the key variables.
KEY variables are placed in the BY statement.
Any 'duplicates' according to the KEY variables are kept in the OUT= dataset
*/
*Generate sample data with duplicate KEY;
data class;
set sashelp.class sashelp.class(obs=5);
run;
@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_format_proc_means_summary.sas
Last active December 17, 2018 18:19
SAS - using a formatted (date) variable to aggregate data
/*Note that if you apply formats to your date they get grouped according to the format, so having a SAS date is advantageous.
Here's a quick example that shows the calculating of yearly statistics from a data set.*/
proc means data=sashelp.stocks N NMISS min max mean median STACKODS;
class date;
format date year4.;
var open high low;
output out=want n= mean= sum= median= mean= / autoname autolabel;
ods output summary = want2;
run;
@statgeek
statgeek / sas_macro_variables_loop
Created August 29, 2017 15:40
SAS - loop through macro variables and resolve
/*Demo to show how to loop through a list of macro variables*/
proc sql noprint;
select name into :name1-
from sashelp.class;
quit;
%macro demo;
%do i=1 %to 10;
%put name&i=&&name&i;
@statgeek
statgeek / sas_export_excel_file_proc_export
Created August 29, 2017 00:57
SAS - Export Data Set to Excel File
/*Sample code that demonstrates how to export an excel file using SAS UE*/
proc export data=sashelp.class outfile='/folders/myfolders/Class.XLSX' dbms=xlsx replace; run;
@statgeek
statgeek / sas_random_number
Created August 27, 2017 18:30
SAS - create random numbers for each observation
/*How to create 50 random numbers for every observation in your data set */
data want;
set sashelp.class;
do i=1 to 50;
zv = rannor(0);
output;
end;
@statgeek
statgeek / sas_split_data_number_records
Created August 27, 2017 18:24
SAS - split dataset into subsets based on number of records
*create a sample data set;
data have;
do i=1 to 7000;
x=rand('bernoulli', 0.4);
output;
end;
run;
*Set group size;
%let group_size = 1000;
@statgeek
statgeek / sas_proc_means_sorted_output.sas
Last active October 5, 2021 20:26
SAS - Proc Means sorted output
*generate fake data;
data have;
array a(17);
do i=1 to 100;
do j=1 to 17;
a(j)=rand('bernoulli', 0.27);
if rand('bernoulli', 0.1)=1 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");