Skip to content
Snippets Groups Projects
lab_2.sas 1.92 KiB
Newer Older
  • Learn to ignore specific revisions
  • Angie Liu's avatar
    Angie Liu committed
    /* ----------------------------------------------------- */
    /* AEDE 6120 */
    /* Lab 2 SAS Basics */
    
    
    * A few things to notice:
     - remember to put a semicolon after your command
     - remember to put a RUN after your step
     - case and format of the code do not matter
     - two ways to add comments;
    
    
    * lab preparation:
     - create the 'aede' folder
     - upload 'CLASS1.csv' and 'CLASS2.xlsx'
     - create a sas program;
     
    
    /* ----------------------------------------------------- */
    
    
    /* Set library path */
    libname aede "/home/u49260161/aede";
    
    
    /* Import data files */
    * example 1: import a csv file as a sas table;
    PROC IMPORT DATAFILE="/home/u49260161/aede/CLASS1.csv"
    	DBMS=csv
    	OUT=aede.class1;
    	GETNAMES=yes;
    RUN;
    
    * example 2: import a xlsx file as a sas table;
    PROC IMPORT DATAFILE="/home/u49260161/aede/CLASS2.xlsx"
    	DBMS=xlsx
    	OUT=aede.class2;
    	GETNAMES=yes;
    RUN;
    
    
    /* Print datasets */
    PROC PRINT DATA=aede.class1; 
    RUN;
    
    
    /* Export data files */
    * export a sas table as a xlsx file;
    PROC EXPORT DATA=aede.class1 OUTFILE="/home/u49260161/aede/class1x.xlsx" 
    	DBMS=xlsx 
    	REPLACE;
    RUN;
    
    
    /* Access SAS tables  */
    DATA work.class1;
    	SET aede.class1;
    RUN;
    
    
    /* Filter rows & select columns */
    DATA work.class1filt;
    	SET aede.class1;
    	WHERE age >= 13 AND sex = 'F';
    RUN;
    
    DATA work.class1keep;
    	SET aede.class1;
    	KEEP name sex age;
    RUN;
    
    DATA work.class1drop;
    	SET aede.class1;
    	DROP sex age;
    RUN;
    
    
    /* Create new variables */
    DATA work.class1newv;
    	SET aede.class1;
    	new_height = height * 2 - 50;
    	new_weight = weight + 20;
    RUN;
    
    
    /* Sort */
    PROC SORT DATA=aede.CLASS1 OUT=work.class1s;
    	BY age name;
    RUN;
    
    PROC SORT DATA=aede.CLASS1 OUT=work.class1s;
    	BY age descending height;
    RUN;
    
    
    /* Merge */
    PROC SORT DATA=aede.CLASS1 OUT=work.sort1;
    	BY Name;
    RUN;
    
    PROC SORT DATA=aede.CLASS2 OUT=work.sort2;
    	BY Name;
    RUN;
    
    DATA aede.class_merge;
    	MERGE sort1 sort2;              *note: 'work' is optional;
    	BY Name;
    RUN;
    
    
    /* Delete */
    PROC DELETE DATA=sort1 sort2;
    RUN;