introduction to sas data set options
DESCRIPTION
Using Base SAS can be powerful with the Data Set Options. This presentation previews helpful uses of this declarative feature.TRANSCRIPT
Introduction to Data Set Options
Mark Tabladillo, Ph.D.Software Developer, MarkTab ConsultingAssociate Faculty University of PhoenixAssociate Faculty, University of Phoenix
January 30, 2007
IntroductionIntroduction
• Data set options allow features duringData set options allow features during dataset processing
• Most SAS data set options can apply to• Most SAS data set options can apply to either input or output SAS data sets in DATA steps or procedure (PROC) stepsDATA steps or procedure (PROC) steps
• Data set options allow the data step to t l i bl b ti itcontrol variables, observations, security,
and data set attributes
OutlineOutline
• Define data set optionsDefine data set options• Provide examples in four categories
Di d t t i l• Discuss data set processing rules
OutlineOutline
• Define data set optionsDefine data set options• Provide examples in four categories
Di d t t i l• Discuss data set processing rules
DefinitionDefinition
• Data set options specify actions thatData set options specify actions that apply only to the SAS data set with which they appearthey appear.
http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a002612367.htm
SyntaxSyntax
• Specify a data set option in parenthesesSpecify a data set option in parentheses after a SAS data set name. To specify several data set options separate themseveral data set options, separate them with spaces. (option 1=value 1< option n=value n>)(option-1=value-1<...option-n=value-n>)
http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a002612367.htm
OutlineOutline
• Define data set optionsDefine data set options• Provide examples in four categories
Di d t t i l• Discuss data set processing rules
Quick ExamplesQuick Examples
• Data set options enable us to performData set options enable us to perform operations such as these:
Renaming variables– Renaming variables– Selecting only the first or last n observations
for processingfor processing– Dropping variables from processing or from
the output data setthe output data set– Specifying a password for a data set– Adding dataset labelsAdding dataset labels
http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a002612367.htm
Common Option CategoriesCommon Option Categories
• Variable ControlVariable Control• Observation Control
S it• Security• Data Set Attributes
Examples DatasetExamples Datasetdata work.sales (drop=i randomState);
length state $2 sales 8 randomState 3;length state $2 sales 8 randomState 3;do i = 1 to 2500;
randomState = round(rand('gaussian',3,1)+0.5);if randomState in (1,2,3,4,5) then do;
l t( d St t )select(randomState);when(1) state='TN';when(2) state='AL';when(3) state='GA';( ) ;when(4) state='FL';when(5) state='MS';
end;sales = int(rand('gaussian' 1000000 500000));sales = int(rand('gaussian',1000000,500000));output work.sales;
end;end;run;
List of Common OptionsList of Common OptionsSAS Data Set Option Description
Variable Control
DROP= Data Set Option
Excludes variables from processing or from output SAS data sets
KEEP= Data Set Option
Specifies variables for processing or for writing to output SAS data p g psets
RENAME= Data Set O i
Changes the name of a variableOption
http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000104210.htm
Examples: Variable ControlExamples: Variable Control
data work salesReformat;data work.salesReformat;set work.sales (drop=sales);run;
data work.salesReformat2;set work.sales (keep=state);run;
proc sort data=work.sales (rename=(state=salesState)) out=work.salesReformat3 (drop=sales);by salesState;run;
List of Common OptionsList of Common OptionsSAS Data Set Option Description
Observation FIRSTOBS= Data Set Specifies which observation SAS Control Option processes first
IN= Data Set Option Creates a variable that indicates whether the data set contributed data to the current observation
OBS= Data Set Option Specifies when to stop processing observationsobse a o s
WHERE= Data Set Option
Selects observations that meet the specified condition
http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000104210.htm
Examples: Observation ControlExamples: Observation Control* (obs - firstobs) + 1 = results; data work.selectObs1;
set work.sales (firstobs=1 obs=200);( );run;
data work.selectObs2;set work.sales (firstobs=200 obs=400);( );run;
proc print data=work.sales (obs=25);run;;
proc freq data=work.sales (firstobs=1);tables state;run;;
proc means data=work.sales (obs=max);class state;var sales;;run;
Examples: Observation ControlExamples: Observation Control
data work combineObs1;data work.combineObs1;set work.selectObs1 (in=in1) work.selectObs2 (in=in2);length source $12;if in1 then source = 'Dataset One';else if in2 then source = 'Dataset Two';run;
data work combineObs2;data work.combineObs2;set work.selectObs1 (in=in1) work.selectObs2 (in=in2);if in1 and in2 then output;run;
List of Common OptionsList of Common OptionsSAS Data Set Option Description
Security ALTER= Data Set Option Assigns an alter password to a SAS file and enables access to a password-protected SAS file
ENCRYPT= Data Set Option
Encrypts SAS data files
PW= Data Set Option Assigns a read, write, or alter password to a SAS file and enables access to aSAS file and enables access to a password-protected SAS file
READ= Data Set Option Assigns a read password to a SAS file and enables access to a read-protected SAS file
WRITE= Data Set Option Assigns a write password to a SAS file and enables access to a write-protected SAS file
http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000104210.htm
SAS file
Examples: SecurityExamples: Securitydata work.secure1 (alter=NoErrors);
set work.sales;run;
data work.secure2;set work sales (alter=NoErrors);set work.sales (alter=NoErrors);run;
* Note: A SAS password does not control access to a SAS file beyond the SAS system You should use the operating system supplied utilities and file systemsystem. You should use the operating system-supplied utilities and file-system security controls in order to control access to SAS files outside of SAS.;data work.secure3 (encrypt=yes pw=Scramble);
set work.sales;run;
proc sort data=work.secure3 (pw=scramble) out=work.secure4;by state sales;y ;run;
List of Common OptionsList of Common OptionsSAS Data Set Option Description
Data Set Attributes
COMPRESS= Data Set Option
Controls the compression of observations in an output SAS data set
GENMAX= Data Set Option
Requests generations for a data set and specifies the maximum number of versions
INDEX D S D fi i d h SAS dINDEX= Data Set Option
Defines indexes when a SAS data set is created
LABEL= Data Set Specifies a label for the SAS data setOption
http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000104210.htm
Examples: Data Set AttributesExamples: Data Set Attributesdata work.compress1 (compress=yes label="Attempt at Compression");
set work.sales;run;
data work masterSalesDataset (genmax=3);data work.masterSalesDataset (genmax 3);set work.sales;run;
d t k t S l D t tdata work.masterSalesDataset;set work.masterSalesDataset work.selectObs1;run;
data work.masterSalesDataset;set work.sales work.selectObs1;run;
OutlineOutline
• Define data set optionsDefine data set options• Provide examples in four categories
Di d t t i l• Discuss data set processing rules
Input and Output DatasetsInput and Output Datasets
• If a data set option is associated with anIf a data set option is associated with an input data set, the action applies to the data set that is being readdata set that is being read.
• If the option appears in the DATA statement or after an output data setstatement or after an output data set specification in a PROC step, SAS applies the action to the output data setthe action to the output data set.
http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a002612367.htm
Input and Output DatasetsInput and Output Datasets
d t lldata _null_;run;
data;;run;
data _null_;set null ;set _null_;if _n_ ge 0 then put 'hello';run;
data _null_;if _n_ ge 0 then put 'hello';set _null_;run;run;
Order of ExecutionOrder of Execution
• When data set options appear on both input andWhen data set options appear on both input and output data sets in the same DATA or PROC step, SAS applies data set options to input data sets before it evaluates programming statements or before it applies data set options to output d t tdata sets.
• Likewise, data set options that are specified for the data set being created are applied afterthe data set being created are applied after programming statements are processed.
http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a002612367.htm
Order of ExecutionOrder of Execution
data work.salesReformat4 (rename=(sales=monthlySales));set work.sales;sales = sales/12;run;run;
data work.salesReformat5;set work.sales (rename=(sales=monthlySales));monthlySales = monthlySales/12;run;
Specification ConflictsSpecification Conflicts
• In some instances data set optionsIn some instances, data set options conflict when they are used in the same statement For example you cannotstatement. For example, you cannot specify both the DROP= and KEEP= options for the same variable in the sameoptions for the same variable in the same statement.
http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a002612367.htm
Statement DefinitionStatement Definition
• A SAS statement is a series of items thatA SAS statement is a series of items that may include keywords, SAS names, special characters and operatorsspecial characters, and operators.
• All SAS statements end with a semicolon. A SAS t t t ith t SAS t• A SAS statement either requests SAS to perform an operation or gives information t th tto the system.
http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a002612375.htm
Timing ConflictsTiming Conflicts
• Timing can also be an issue in someTiming can also be an issue in some cases. For example, if using KEEP= and RENAME= on a data set specified in theRENAME on a data set specified in the SET statement, KEEP= needs to use the original variable names because SAS willoriginal variable names, because SAS will process KEEP= before the data set is read The new names specified inread. The new names specified in RENAME= will apply to the programming statements that follow the SET statementstatements that follow the SET statement.
http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a002612367.htm
Timing ConflictsTiming Conflicts
proc sort data=work.sales (keep=sales state rename=(sales=monthlySales))
out=work salesReformat6;out=work.salesReformat6;by state monthlySales;run;
proc sort data=work.sales (rename=(sales=monthlySales) keep=sales state)
out=work.salesReformat7;by state monthlySales;by state monthlySales;run;
Overriding System OptionsOverriding System Options
• Many system options and data set optionsMany system options and data set options share the same name and have the same function.
• The data set option overrides the system option for the data set in the step in which p pit appears.
• System options remain in effect for all y pDATA and PROC steps in a SAS job or session, unless they are respecified.
http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a002612367.htm
ConclusionConclusion
• DATA set options allow features duringDATA set options allow features during data step processing
• The SAS System Documentation provides• The SAS System Documentation provides specific details on the syntax
Contact InformationContact Information
• Mark TabladilloMark TabladilloMarkTab Consultinghttp://www marktab com/http://www.marktab.com/