



















Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!




















What is SAS? SAS is a very powerful and structured statistical software, built with “packages”. Is built around 4 data driven tasks:
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:
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
•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).
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.
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:
Sas detects the end of a step when it encounters:
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:
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:
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.
Selecting observations:
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?
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
[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:
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
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
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
→ 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
A format name (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
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:
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