SAS Base Programming study materials
SAS Base Programming study resources

SAS Base Programming: Access and Create Data Structures

SAS Base Programming · Work through your exam, one topic at a time.

All guide sections

Access and Create Data Structures

Libraries, data sets, and attributes

SAS tables live in libraries. A libref is a temporary alias created with a LIBNAME statement (or assigned in a metadata environment) that points to a folder; WORK is the session library that is deleted when SAS exits. Tables are addressed with two-level names, libref.member. The DATA statement creates a table, SET reads one, MERGE combines tables, INFILE/INPUT read raw text files, and PROC IMPORT guesses structure from delimited files, Excel workbooks, and databases.

Variable attributes are type, length, format, informat, and label. A LENGTH or ATTRIB statement must appear before the SET statement that brings in the variable, otherwise the length carried by the incoming data set wins and your new value gets truncated.

Traps that show up in questions

Data set options written in parentheses - (KEEP= DROP= RENAME= WHERE= FIRSTOBS= OBS= IN=) - apply only to that one reference, while the corresponding statements (KEEP, DROP, RENAME, WHERE) apply to the whole step. WHERE= filters before variables are read, so it can reference only variables present in that source table. SET stacks rows; MERGE joins columns; MERGE with a BY statement needs sorted (or indexed) input, and without BY it matches observations by position. A one-to-many MERGE without BY produces the "repeated BY values" note and misleading results. Member names must start with a letter or underscore, contain only letters, digits, underscores, and be at most 32 characters.

Rule of thumb

Options inside parentheses travel with the data set name; statements apply to the entire DATA step.

data work.pass;
  set raw.scores(keep=id name score where=(score>90));
run;