Docsity

Statistical Software, Appunti di Statistica

Toggle sidebar
bg1
SAS
What is SAS?
SAS is a very powerful and structured statistical software, built with “packages”.
Is built around 4 data driven tasks:
1. Access
2. Manage
3. Present
4. Analyze
Sas has a program and an interface. Te interface consists of 3 principal windows:
1. program editor → main instructions written following SAS syntax rules
2. output → visual results of the procedure
3. log → info about the elaboration
A SAS program is a sequence of one or more steps.
DATA steps → create SAS data sets
→ used for the data import from an external file and for the creation of new SAS dataset
PROC steps → process SAS data sets to generate reports and graphs and to manage data
→ many procedures: perform data analysis, import data with different format than one of a
SAS dataset, print/visualize data and result produced by other procedures
SAS datasets are stored in SAS libraries → collection of SAS files that are referenced and
stored as a unit. A SAS dataset is one file in the “drawer” that is SAS a SAS library
Each SAS session creates at least one temporary and one permanent SAS library
Temporary library: WORK is a temporary library where you can store and access SAS data
sets for the duration of the SAS session. When each session terminates, SAS deletes the
work library. It’s the default library
Permanent library: SASHELP is a permanent library that contains sample SAS data sets
you can access during your SAS session
SASUSER: is a permanent library that you can use to store and access SAS datasets in any
SAS session
SAS datasets in a permanent library are saved after your SAS session terminates.
LIBREF → shortcut to the library. It’s a logical name (called library reference name) to refer
to a SAS library.
SAS datasets have a two-level name that consists of the libref and the dataset name,
separated by a period.
When a dataset is stored in the temporary work library, you can use a one level name (only
name of the dataset)
User defined libraries → created by users. Is permanent, implemented within the operating
environment’s file system. 2 step:
Sep 1 → assign a libref to the user-defined library to make it available during a SAS session
Step 2 → use a SAS LIBNAME statement to associate the libref with the physical location of
the library
The SAS libname statement is a global SAS statement:
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Anteprima parziale del testo

Scarica Statistical Software e più Appunti in PDF di Statistica solo su Docsity!

SAS

What is SAS? SAS is a very powerful and structured statistical software, built with “packages”. Is built around 4 data driven tasks:

  1. Access
  2. Manage
  3. Present
  4. Analyze Sas has a program and an interface. Te interface consists of 3 principal windows:
  5. program editor → main instructions written following SAS syntax rules
  6. output → visual results of the procedure
  7. log → info about the elaboration

A SAS program is a sequence of one or more steps. DATA steps → create SAS data sets → used for the data import from an external file and for the creation of new SAS dataset

PROC steps → process SAS data sets to generate reports and graphs and to manage data → many procedures: perform data analysis, import data with different format than one of a SAS dataset, print/visualize data and result produced by other procedures

SAS datasets are stored in SAS libraries → collection of SAS files that are referenced and stored as a unit. A SAS dataset is one file in the “drawer” that is SAS a SAS library Each SAS session creates at least one temporary and one permanent SAS library

Temporary library : WORK is a temporary library where you can store and access SAS data sets for the duration of the SAS session. When each session terminates, SAS deletes the work library. It’s the default library Permanent library : SASHELP is a permanent library that contains sample SAS data sets you can access during your SAS session SASUSER: is a permanent library that you can use to store and access SAS datasets in any SAS session SAS datasets in a permanent library are saved after your SAS session terminates.

LIBREF → shortcut to the library. It’s a logical name (called library reference name) to refer to a SAS library. SAS datasets have a two-level name that consists of the libref and the dataset name, separated by a period. When a dataset is stored in the temporary work library, you can use a one level name (only name of the dataset)

User defined libraries → created by users. Is permanent, implemented within the operating environment’s file system. 2 step: Sep 1 → assign a libref to the user-defined library to make it available during a SAS session Step 2 → use a SAS LIBNAME statement to associate the libref with the physical location of the library The SAS libname statement is a global SAS statement:

  • it is not required to be in a DATA step or PROC step
  • it does not require a RUN statement
  • it execute immediately
  • it is effective until changed or cancelled, or until the session ends

DATASETS → is a structured data file that SAS creates and that only SAS can read. Table that contains observations and variables.

To create a new dataset using an external dataset: data import procedures that can be implemented To create a new dataset relying on an existing dataset: the SET statement is used → creates a new dataset with the same variables and observations of the original dataset → user can add new variables and observations

data a;

set data.example;

heightCM=heightM*100;

run;

•Using this code a new dataset (a) with the same variables and observations of the dataset example of the library data is created. •In addiction to, the new variable heightCM is created, its value is multiplied by 100 the value of the variable heightM (present in the example dataset).

  • heightCM is now created in the original dataset ( data.example ).

It is also possible to append observations (records) to a dataset derived from another dataset. Variables names in the two datasets to combine should be the same.

data a;

set data.example1 data.example2;

run;

For variables present only in one of the 2 datasets (A or B) missing values are created for the observations that belong to the dataset where the variable is not present.

SAS steps begin with either:

  • a DATA statement
  • a PROC statement

Sas detects the end of a step when it encounters:

  • a RUN statement

It is possible to import files also using the PROC IMPORT procedure.

Dataset structure A SAS dataset contains a descriptor portion and a data portion. The descriptor portion contains the following metadata:

  • general properties (dataset names, number of observations,...)
  • variable properties (type, length,...) The data portion of a SAS dataset contains the data values, which are either character or numeric SAS variable names
  • can be 1 to 32 characters long
  • must start with a letter or underscore. Subsequent characters can be letters, underscores or numerals (NO SPECIAL CHAR.)
  • can be uppercase, lowercas
  • e or mixed case
  • ARE NOT CASE SENSITIVE LABEL Is another attribute of each variable contained in a dataset The user can assign a label to the variable in order to clarify its content In an open dataset, if you look at the column headings you will find labels instead. From the programming side the user must still refer to the variable name The label statement can be used in every DATA step in order to assign a label to one or more variables

Variable Type

1. Character variables

○ Can contain any value: letters, numerals, special characters and blanks ○ Range from 1 to 32767 characters in length ○ Have 1byte per character

2. Numeric variables ○ Store numeric values using floating point or binary representation ○ Have 8 bytes of storage by default ○ Can store 16 or 17 significant digits (cifre) Missing values Are valid values in a SAS dataset. A blank represents a missing character value, a period represents a missing numeric value.

PROC CONTENTS Procedure used to visualize the content and the structure of a dataset. The output can be found in the output SAS window. Syntax:

proc contents data=; run; Output:

  • The first part contains general information about the dataset (name, date of creation, number of obs,..)
  • The second part reports the variable characteristics that are in the dataset (name, type, format, position in the dataset, label,...)

PROC PRINT Procedure used to display the data portion of a SAS dataset.

NOOBS option : used to suppress the Obs column (colonna tutta a sinistra)

ID statement : specifies the variable or variables to print at the beginning of each row instead of an observation number. → choose ID variables that uniquely identify observations!

PROC SORT

We can use the sort procedure to create a new dataset ordering the observations by a certain value. We can then use proc print to display the sorted dataset. Rearranges the observations in the input data set based on the values of the variable or variables listed in the BY statement.

→ the input dataset is overwritten unless the OUT = option is used to specify an output data set → can sort on multiple variables → sort in ascending (default) or descending order → does not generate printed output

→ The DESCENDING option reverses the sort order for the variable that immediately follows it

→ The observations are sorted from the largest value to the smallest value

→ PROC SORT first arranges the data set by the values of the first BY variable, then arranges any observations that have the same value of the first BY variable by the values of the second BY variable.

  • you can remove one or more variables from the dataset by using the DROP statement followed by the variable name (or the group of variables)
  • this statement has to be written within a DATA step
  • alternatively we can use the KEEP statement instead of the DROP statement → all the variables that are written after the statement are included in the dataset

Selecting observations:

  • you can select observations by using IF or WHERE
  • these are conditional statements that specify which observations to keep in the dataset for a certain verified condition
  • You can compare the value of a variable with a constant (or the value of another variable). The most used operators for comparisons are: - = (Equal) or EQ - ^= (Not Equal) or NE - > (Greater than) or GT - < (Less than) or LT - >= (Greater than or Equal) or GE - <= (Less than or Equal) or LE
  • you can also compare the value of the variable with a group of values using the IN operator

We can also use AND/OR:

→ OR logic operator: is sufficient that ONE of the conditions is verified in order to keep the observation.

→ AND logic operator: ALL the conditions must be verified in order to keep the observation.

IF or WHERE?

  • If can be used only in a DATA step, WHERE can be used both in DATA step and PROC step (using the WHERE statement in a PROC step, only the observations that meet the specified condition are taken into consideration by the procedure)
  • The only action that the WHERE statement can do on the basis of the specified condition is to select or not the observation. Using the IF statement with THEN keyword, the user can define also different actions from the selection/not selection of observations

The default action of IF is output.

If you specify DELETE as action, SAS will delete the observation from the new dataset.

Creating new variables

  • the instruction to create a new variable is: variable=expression;
  • inside expression we can use mathematical operators or we can assign a constant
  • for missing values for numeric variables the output for that row will be missing

[We can create 2 dataset in a single data step → DATA table1 table2]

Functions solving begins with the most internal one.

FIRST. and LAST. VARIABLE

When you create a new dataset, it is possible to combine the SET statement with the BY statement, starting from an ordered original dataset.

Using the BY statement SAS creates internally 2 variables:

  1. First.variable → is equal to 1 if the current observation is the first show of a value for that variable, else it is equal to 0
  2. Last.variable → is equal to 1 if the current observation is the last show of a value for that variable, else it is equal to 0

The use of first. and last. is useful if you need to identify repeated records for the value of a variable.

Informat and format

Informats are instructions for reading data into a SAS dataset (input data)

Formats are instructions that work on the visualization of the data values into a SAS dataset (or report) (output data)

→ they use a common syntax

→ large quantity available from SAS

Informat

Is an instruction that SAS uses to read data values into variable.

Character informats:

Numeric informats:

Example:

123 → INFORMAT: 3.0 → 123

123 → INFORMAT: 3. → 123

123 → INFORMAT: 3.1 → 12.

If the value that has to be read already contains the comma, SAS ignores the portion d of the INFORMAT:

1.23 → INFORMAT: 4.1 → 1.

1.23 → INFORMAT: 3.1 → 1.

ALSO THE COMMA HAS TO BE COUNTED IN THE COLUMNS

Dates

  • SAS stores dates as numeric variables, since operations as the difference or the sum btw dates are very common. If the variables are stores as character variables (ex.: the string “09/10/09”) operations would not be possible
  • Dates are store as days passed from 1 January 1960. Dates that precede this date correspond to negative numbers
  • We use INFORMATS because it is not handy to insert dates like this

Proc print data=h; FORMAT height 3.1 date date7.; RUN;

→ If you want to give the same format to different variables, you can use the statement as follows:

FORMAT date_a date_b date7. instead of

FORMAT date_a date7. date_b date7.

→ We can define new FORMAT in addition to the standard ones using the PROC FORMAT procedure

From character to numeric and viceversa

INPUT(original_variable, informat) INPUT(source, informat.)

→ Converts character values to numeric values using a specified informat:

→ Returns the value that is produced when SAS converts an expression by using specified informat.

source: specifies a character constant, variable, or expression to which you want to apply specific informat

informat: is the SAS informat that you want to apply to the source

  • this argument must be followed by a period
  • cannot be a character constant, variable, or expression

PUT(original_variable, format) PUT(source, format.)

→ Converts numeric to character values using a specified format:

→ Returns a value using a specified format

source: identifies the constant, variable, or expression whose value you want to reformat. Can be character or numeric.

format: is the SAS format that you want to apply to the source

  • this argument must be followed by a period and optional width and decimal specifications
  • cannot be a character constant, variable, or expression

→ If the PUT function returns a character value to a variable that has not yet been assigned length, the variable length is determined by the width of the format. The same goes for INPUT function.

User defined format

  1. Use proc format procedure
  2. Apply it using FORMAT statement in a report

A format name (VALUE STATEMENT):

  • can be up to 32 characters in length
  • for character formats, it must begin with a dollar sign ($), followed by a letter or underscore
  • for numeric formats, it must begin with a letter or underscore
  • It cannot end with a number
  • It cannot be named with a SAS format name
  • It cannot include a period in the VALUE statement.

Concatenating Data Sets

Use DATA STEP to concatenate the datasets and list the data sets in the SET statement.

The SET statement reads observations from each data set in the order in which they are listed. (We can include any number of dataset in the SET statement)

In order to align columns we have to RENAME VARIABLES in one or more data sets.

The RENAME statement changes the name of a variable

The RENAME= option must be specified in parenthesis immediately after the appropriate SAS data set name.

The name change affects the output data set only, it has no effect on the input data set.

Merging Data sets One-To-One

There are different types of match-merging, here are three different types.

1. One to one → A single obs in one dataset is linked to exactly one obs in

another data set based on the values of one or more selected variables

2. One to Many → a single obs in one dataset is linked to more than one obs in

another data set based on the values of one or more selected variables

3. Non Matches → at least one observation in one data set is linked to no

observation in another data set based on the values of one or more selected

variables

Data sets in a match-merge must be sorted by the common variable or variables being matched:

The MERGE statement in a DATA step joins observations from two or more SAS data sets into a single observation. The BY statement indicates a match-merge and lists the variable(s) to match.

Requirements for match-merging:

  • Two or more data sets listed in the MERGE statement
  • The variables in the BY statement must be common to all data sets
  • The data sets must be sorted by the variables listed in the BY statement

Merging Data sets One-To-MANY

In this one-to-many merge, each observation in dataset1 matches one or more observations in dataset2.

The common variable is called the BY variable The value of the BY variable is the BY value A group of observations with the same BY value is a BY group