SAS Cheat Sheet

Here’s a SAS (Statistical Analysis System) cheat sheet covering syntax and concepts:

Data Step

Reading Data:

data dataset_name;
  infile 'path/to/data.csv' delimiter=',';
  input variable1 variable2 variable3;
run;

Creating Variables:

data dataset_name;
  set dataset_name;
  new_variable = variable1 + variable2;
run;

Proc SQL

Creating a Table:

proc sql;
  create table new_table as
  select variable1, variable2
  from existing_table
  where condition;
quit;

Joining Tables:

proc sql;
  create table joined_table as
  select a.*, b.*
  from table1 a
  inner join table2 b
  on a.id = b.id;
quit;

Proc Means

Descriptive Statistics:

proc means data=dataset_name;
  var variable1;
  class categorical_variable;
run;

Proc Freq

Frequency Distribution:

proc freq data=dataset_name;
  tables categorical_variable;
run;

Proc Print

Print Data:

proc print data=dataset_name;
run;

Data Exploration

Sorting Data:

proc sort data=dataset_name;
  by variable1;
run;

Filtering Data:

data new_dataset;
  set dataset_name;
  where variable1 > 100;
run;

Macro Variables

Defining Macro Variables:

%let variable_name = value;

Using Macro Variables:

data new_dataset;
  set dataset_name;
  if variable1 > &variable_name;
run;

Graphs with Proc SGPLOT

Scatter Plot:

proc sgplot data=dataset_name;
  scatter x=variable1 y=variable2;
run;

Histogram:

proc sgplot data=dataset_name;
  histogram variable1;
run;

Error Checking

Checking for Missing Values:

data dataset_name;
  set dataset_name;
  if missing(variable1) then variable1 = 0;
run;

Checking for Duplicates:

proc sort data=dataset_name nodupkey;
  by variable1 variable2;
run;

Output Management

Exporting Data:

proc export data=dataset_name
  outfile='path/to/output.csv'
  dbms=csv replace;
run;

Saving Output:

ods html file='path/to/output.html';
proc print data=dataset_name;
run;
ods html close;

This SAS cheat sheet covers basic syntax for data manipulation, analysis, and visualization. Refer to the official SAS documentation for more in-depth information and advanced features.