Presentation is loading. Please wait.

Presentation is loading. Please wait.

SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 3 & 4 By Tasha Chapman, Oregon Health Authority.

Similar presentations


Presentation on theme: "SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 3 & 4 By Tasha Chapman, Oregon Health Authority."— Presentation transcript:

1 SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 3 & 4 By Tasha Chapman, Oregon Health Authority

2 Topics covered…  SAS libraries  Reading data from external files  txt and csv  Filename statement  Datalines  SET statement  Basic PROC Print  Basic PROC Contents  Basic PROC Freq  Basic PROC Means

3 SAS libraries

4  LIBNAME statement assigns a libref  Libref (short for “Library Reference”) is an alias or nickname for a directory or folder for SAS datasets

5 SAS Datasets: Permanent location of all SAS Datasets SAS Datasets: Permanent location of all SAS Datasets Text and CSV: Text and CSV data files used to create the SAS Datasets Text and CSV: Text and CSV data files used to create the SAS Datasets Would assign libref using LIBNAME statement

6 LIBNAME statement: Assigns a libref Libref is an alias for a directory or folder where you store permanent SAS datasets Libref can be anything you choose Libref only exists for current SAS session LIBNAME statement: Assigns a libref Libref is an alias for a directory or folder where you store permanent SAS datasets Libref can be anything you choose Libref only exists for current SAS session SAS libraries

7  LIBNAME statement assigns a libref  Libref (short for “Library Reference”) is an alias or nickname for a directory or folder for SAS datasets  Dataset references contain two parts:  libref  dataset-name  Looks like: libref.dataset-name  If libref is blank, the default is the Work library

8 Dataset reference: Consists of two parts – Libref.dataset-name mozart.test_scores is short for c:\books\learning\test_scores Default is Work Dataset reference: Consists of two parts – Libref.dataset-name mozart.test_scores is short for c:\books\learning\test_scores Default is Work SAS libraries

9 SAS work library  Work is a temporary library  SAS datasets created in Work only exist during SAS session  Once SAS session ends, datasets are erased  Do not need to assign a libref for Work or specify it in dataset references data Test_Scores; is the same as data work.Test_Scores;

10 LIBNAME statement: Assigns a libref Use the libref for saving data and for retrieving data LIBNAME statement: Assigns a libref Use the libref for saving data and for retrieving data SAS libraries

11 Explorer Window: See libraries and SAS datasets Explorer Window: See libraries and SAS datasets

12 Active Libraries: Double click on a library to see the datasets in it Active Libraries: Double click on a library to see the datasets in it

13 LIBNAME examples Oops! Your password is showing! Oops! Your password is showing!

14 LIBNAME trick  Save your commonly used and/or passworded LIBNAME statements in a text file (using Notepad)  Use a %include statement to reference the text file at the beginning of every SAS program SAS will include the code in the text file as if it were part of your program.

15 Reading external data

16 Four variables: Gender, Age, Height (in inches), Weight (in pounds) Variables separated by blanks Four variables: Gender, Age, Height (in inches), Weight (in pounds) Variables separated by blanks Reading data from a text file

17 INFILE – where to find the data INPUT – variable names to associate with each data value ($ indicates character variable. Otherwise numeric.) INFILE – where to find the data INPUT – variable names to associate with each data value ($ indicates character variable. Otherwise numeric.) Reading data from a text file

18 Results of PROC Print of “Demographics” Obs – short for “observation” (part of PROC Print output) Numbers observations from 1 to N Results of PROC Print of “Demographics” Obs – short for “observation” (part of PROC Print output) Numbers observations from 1 to N Reading data from a text file

19 Four variables: Gender, Age, Height (in inches), Weight (in pounds) Variables separated by commas Four variables: Gender, Age, Height (in inches), Weight (in pounds) Variables separated by commas Reading data from a csv file

20 dsd option (delimiter-sensitive data):  Changes default delimiter from blank to comma  If two delimiters in a row, assumes missing value between  Quotes stripped from character values dsd option (delimiter-sensitive data):  Changes default delimiter from blank to comma  If two delimiters in a row, assumes missing value between  Quotes stripped from character values Reading data from a csv file

21 Results of PROC Print of “Demographics” SAS data results are the same Results of PROC Print of “Demographics” SAS data results are the same Reading data from a csv file

22 Other delimiters  Use the dlm= (or delimiter= ) option to specify data delimiters other than blanks or commas  Example: infile 'D:\Data\mydata.txt' dlm=':';  Can use dsd and dlm= options together  Performs all functions of dsd, but overrides default delimiter

23 Filename  FILENAME statement assigns a fileref  Fileref (short for “File Reference”) is an alias or nickname for an external file

24 Filename  Useful when you need to read two or more files with same format (such as quarterly data)

25 Datalines  Allows dataset to be created within SAS program  Can be useful for creating a quick set of test data  Use either datalines or cards options  Follow with semi-colon after last line of data

26 SET statement

27  After you’ve brought your data into a SAS dataset, most of your DATA steps will look like this: SET statement Creates a new dataset called “Females” Uses previous dataset “AllData” as the basis of the new dataset Applies these modifications to the new dataset

28 SET statement  The SET statement is similar to an INPUT statement  Except instead of a raw data file, you are reading observations from a SAS dataset  Can read in temporary or permanent SAS datasets

29 PROC Print

30  PROC Print can be used to list the data in a SAS dataset

31 Results of PROC Print of “Demographics” PROC Print

32  Many options to control output of PROC Print  noobs – Suppresses “OBS” column in output  (obs=2) – Only prints the first two observations Can put in any number: 1 through N Must be placed in parentheses after data= option  var statement – Only prints listed variables

33 We’ll discuss other PROC Print options in later chapters PROC Print

34 PROC Contents

35  PROC Contents can be used to display the metadata (descriptor portion) of the SAS dataset

36 PROC Contents Results of PROC Contents of “Demographics”

37 PROC Contents Number of observations and variables Variable list Dataset name File name

38 PROC Contents variable list  # - Variable number (varnum)  Variable – Name of variable  Type – Numeric or Character  Len – Variable length  Format – How the data is displayed  Informat – How the data was read by SAS

39  Variables listed in alphabetical order by default  Uppercase alphabetized before lowercase (e.g., “ZZTOP” would be alphabetized before “aerosmith”)  Use the varnum option to list variables in order they were created in PROC Contents variable list

40 PROC Freq

41  PROC Freq can be used to run simple frequency tables on your data

42 PROC Freq Results of PROC Freq of “Demographics”

43  Use the table statement to only print selected variables  Use the nocum option to suppress cumulative statistics  Use the nopercent option to suppress percent statistics  Can use options together or separately PROC Freq

44  Can create simple cross-tabulations

45 PROC Freq  Use the list option to display cross-tab tables in a list format

46 PROC Means

47  PROC Means can be used to run simple summary statistics on your data

48 Results of PROC Means of “Demographics” PROC Means

49  Many options to control output of PROC Means  NMiss Mean Median – Examples of statistics that can be specified in PROC Means (see later slide for list of statistical keywords)  class statement – Allows for grouping by categorical variables  var statement – Only provides statistics for listed analysis variables

50 We’ll discuss other PROC Freq and PROC Means options in later chapters PROC Means

51 Examples of statistics that can be run with PROC Means

52 Read chapters 5 & 6 and sections 3.9 through 3.14 For next week…


Download ppt "SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 3 & 4 By Tasha Chapman, Oregon Health Authority."

Similar presentations


Ads by Google