Sas Cheat Sheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

SAS CHEAT SHEET

DATA STATEMENT PROC STATEMENT

1. Modifying data- Numerical changes 1. STRUCTURE OF THE DATA

DATA LIBNAME.NEWDATA; PROC CONTENTS LIBNAME.DATASET;


SET LIBNAME.OLDDATA; RUN;
FORMAT X Y 10.2;
X= LOG (AGE); Y= HEIGHT^2; RUN; 2. TO VIEW THE DATA IN THE OUTPUT
WINDOW
2. STRING FUNCTIONS
PROC PRINT DATA LIBNAME.NEW;
To get the Length of the variable VAR VARIABLE1 VARIABLE2; RUN;
LEN(Variable)
To concatenate two variables TO OBSERVE THE FIRST 10 OBS.
Variable1||Variable2
To remove extraneous spaces PROC PRINT DATA NEW (OBS = 10);
STRIP(Variable) RUN;
COMPRESS(var1||var2)

3. SUBSTRING FUNCTIONS 3. SORTING

 To extract 3 objects from the first position BY VARIABLE


SUBSTR(VAR, 1, 3) PROC SORT DATA = LIBNAME.NEW;
 UPCASE(Var)/LOWCASE(Var) BY VAR1 DESCENDING VAR2;RUN;
 Extraction from a variable[Value=123]
SCAN (VAR, 1, 3) 4. DESCRIPTIVE STATISTICS
RESULT- 12 [BY DEFAULT A SUMMARY IS CREATED
IN THE OUTPUT WINDOW]
4. FILTERING DATA PROC MEANS DATA=LIB.DATA; RUN;
FOR A SPECIFIC MEASURE
KEEP /DROP PROC MEANS DATA=LIB.DATA MEAN;
DATA LIB.NEW (DROP = VAR1 VAR2); RUN;
SET LIB.OLD (KEEP = VAR1 VAR2 VAR3 VAR4);
RUN; FOR A SPECIFIC CONDITION
PROC MEANS DATA=LIB.DATA;
5. DATE FUNCTIONS WHERE SEX= “F”;RUN;
DAY(A)
MONTH(A) BY STATEMENT
YEAR(A) PROC MEANS DATA=LIB.DATA MEAN;
QTR(A) BY SEX; RUN;
WEEKDAY(A)
MDY(Y, X, Z) CLASS STATEMENT
TODAY() PROC MEANS DATA=LIB.DATA MEAN;
CLASS SEX; RUN;
To know the End Date when start date and duration is known
INTNX(“UNIT”, START DATE, DURATION)
To get the date of the end of the month/week PROC SUMMARY DATA=LIB.DATA MEAN;
INTNX(“UNIT”, START DATE, DURATION,’E’) VAR X; CLASS SEX;
To get the date of the start of the month/week OUTPUT OUT= LIB.OUT; RUN;
INTNX(“UNIT”, START DATE, DURATION,’S’)

To know the duration when End Date and Start Date is given
INTCK("UNIT", DATE1, DATE2)
6. IF-THEN-ELSE 5. SQL IN SAS

DATA NEW; SET OLD; PROC SQL;


IF CONDITION THEN ACTION; CREATE TABLE DATANAME AS SELECT *
ELSE IF CONDITION THEN ACTION; FROM SOURCE DATA
ELSE IF CONDITION THEN ACTION; WHERE CONDITION; QUIT;
....;
ELSE ACTION; RUN; DISTINCT/COUNT

7. MERGING PROC SQL;


CREATE TABLE DATANAME AS SELECT
 X&Y COUNT(DISTINCT(VAR)) AS CNT
DATA LIB.M; FROM SOURCE DATA; QUIT;
MERGE LIB.A(IN = X) LIB.C(IN = Y);
BY M; MERGING USING SQL
IF X & Y; RUN;
PROC SQL;
 X & NOT(Y) CREATE TABLE DATANAME AS SELECT *
DATA LIB.M; FROM DATA1.A X DATA2.B Y
MERGE LIB.A(IN = X) LIB.C(IN = Y); WHERE X.M=Y.L;QUIT;
BY M; [UNIQUE COLUMN OF X IS M AND Y IS M]
IF X & NOT(Y); RUN;

 IF X 6. CREATING MACROS
DATA LIB.M;
MERGE LIB.A(IN = X) LIB.C(IN = Y); %MACRO UNI(DT, VA, OB)
BY M; PROC UNIVARIATE DATA= &DT
IF X; RUN; NEXTROBS= &OB; VAR &VA;RUN;MEND;

 IF Y 7. REMOVAL OF DUPLICATES
DATA LIB.M;
MERGE LIB.A(IN = X) LIB.C(IN = Y); BY VARIABLES
BY M; PROC SORT DATANAME NODUPKEY;
IF Y; RUN; OUT= LIB.O DUPOUT= LIB.D; BY SEX;
RUN;

BY ALL OBSERVATIONS
PROC SORT DATANAME NODUP;
OUT= LIB.O DUPOUT=LIB.D;BY SEX;
RUN;

8. CREATING PLOT

PROC UNIVARIATE DATANAME PLOT;

VAR A; RUN;

PROC GPLOT DATANAME; PLOT A*B; RUN;

You might also like