Sas Data Statement
Sas Data Statement
Sas Data Statement
DATA STATEMENT
Begins a DATA step and provides names for output SAS data sets
Dataset names should write according to SAS Naming rules.
Dataset Options
KEEP:-
Specifies variables for processing or for writing to output SAS data sets
Syntax: - KEEP=variable<s>
22
Examples: -
Data ds1 (keep=name sal);
Page
Infile datalines;
Input id name$ sex$ age sal;
Datalines;
001 abc m 23 45000
002 def f 34 67000
003 mno m 21 36000
004 xyz f 27 45000
;
Run;
__________________________________________________________________________________________
STANSYS SOFTWARE SOLUTIONS
#7-1-621/113(67/3RT), Beside: Nagarjuna High School, Near: S.R.Nagar Community Hall,
S.R.Nagar, Hyd-38|Ph:9542195422/8125980000|www.stansys.co.in|sas@stansys.co.in
Data ds;
Infile datalines;
Input id name$ sex$ age sal;
Datalines;
001 abc m 23 45000
002 def f 34 67000
003 mno m 21 36000
004 xyz f 27 45000
;
Run;
Data ds1a (keep=name sal);
Set ds;
Run;
Data ds1b (keep=name sal);
Infile "C:\Documents and Settings\stansys\Desktop\sample.txt";
Input id name$ sex$ age sal;
Run;
Proc import datafile ="C:\Documents and Settings\stansys\Desktop\sample.xls"
Out=work.ds(keep=name sal)
dbms=excel
replace;
Run;
DROP: -
Excludes variables from output SAS data sets
Syntax: DROP variable<s>
Examples: -
Data ds2 (Drop=name sal);
Infile datalines;
Input id name$ sex$ age sal;
Datalines;
001 abc m 23 45000
002 def f 34 67000
003 mno m 21 36000
004 xyz f 27 45000
;
23
Run;
Data ds;
Page
Infile datalines;
Input id name$ sex$ age sal;
Datalines;
001 abc m 23 45000
002 def f 34 67000
003 mno m 21 36000
004 xyz f 27 45000
;
Run;
__________________________________________________________________________________________
STANSYS SOFTWARE SOLUTIONS
#7-1-621/113(67/3RT), Beside: Nagarjuna High School, Near: S.R.Nagar Community Hall,
S.R.Nagar, Hyd-38|Ph:9542195422/8125980000|www.stansys.co.in|sas@stansys.co.in
Data ds2a (drop=name sal);
Set ds;
Run;
Data ds2b (drop=name sal);
Infile "C:\Documents and Settings\stansys\Desktop\sample.txt";
Input id name$ sex$ age sal;
Run;
Proc import data file="C:\Documents and Settings\stansys\Desktop\sample.csv"
Out=work.ds(drop=name sal)
dbms=csv
replace;
Run;
RENAME: -
Specifies new names for variables in output SAS data sets
Syntax: -
RENAME= (old-name-1=new-name-1 . . . <old-name-N=new-nameN>);
Examples: -
Data ds3 (rename=(sex=gender sal=income));
Infile datalines;
Input id name$ sex$ age sal;
Datalines;
001 abc m 23 45000
002 def f 34 67000
003 mno m 21 36000
004 xyz f 27 45000
;
Run;
Data ds3 (rename=(sex=gender sal=income));
Infile "C:\Documents and Settings\Stansys\Desktop\sample.txt";
Input id name$ sex$ age sal;
Run;
Data ds;
Infile datalines;
Input id name$ sex$ age sal;
24
Datalines;
001 abc m 23 45000
Page
__________________________________________________________________________________________
STANSYS SOFTWARE SOLUTIONS
#7-1-621/113(67/3RT), Beside: Nagarjuna High School, Near: S.R.Nagar Community Hall,
S.R.Nagar, Hyd-38|Ph:9542195422/8125980000|www.stansys.co.in|sas@stansys.co.in
Proc import data file="C:\Documents and
Settings\stansys\Desktop\sample.csv"
Out=work.ds (rename=(sex=gender
sal=income))
dbms=csv
replace;
Run;
WHERE: -
Selects observations from SAS data sets that meet a particular condition
Syntax: - where= (Arguments)
Examples: -
Data ds4 (where= (sex='m'));
Infile datalines;
Input id name$ sex$ age sal;
Datalines;
001 abc m 23 45000
002 def f 34 67000
003 mno m 21 36000
004 xyz f 27 45000
;
Run;
Data ds;
25
Infile datalines;
Input id name$ sex$ age sal;
Page
Datalines;
001 abc m 23 45000
002 def f 34 67000
003 mno m 21 36000
004 xyz f 27 45000
;
Run;
Data ds4a (where= (age>=23));
Set ds;
__________________________________________________________________________________________
STANSYS SOFTWARE SOLUTIONS
#7-1-621/113(67/3RT), Beside: Nagarjuna High School, Near: S.R.Nagar Community Hall,
S.R.Nagar, Hyd-38|Ph:9542195422/8125980000|www.stansys.co.in|sas@stansys.co.in
Run;
Data ds4b (where= (age=23 and sex='m'));
Set ds;
Run;
Data ds4c (where= (id in (001,004)));
Infile "C:\Documents and Settings\stansys\Desktop\sample.txt";
Input id name$ sex$ age sal;
Run;
Proc import data file="C:\Documents and Settings\stansys\Desktop\sample.xls"
Out=work.ds(where=(sex='f'))
dbms=excel replace;
Run;
REPLACE: -
Controls replacement of like-named temporary or permanent SAS data sets
When we are creating dataset with any name that dataset already is exist in our SAS
library, by default it will replace on first dataset when second data step executes,
but when don’t want to replace use replace=No
Default replace=Yes.
Syntax: - REPLACE=NO | YES
Examples: -
Data ds5 (where= (sex='m'));
Infile datalines;
Input id name$ sex$ age sal;
Datalines;
001 abc m 23 45000
002 def f 34 67000
003 mno m 21 36000
004 xyz f 27 45000
;
Run;
Data ds5 (replace=no);
Infile datalines;
Input id name$ sex$ age sal;
26
Datalines;
001 abc m 23 45000
Page
PW=PASSWORD: -
It’s assigns a password to dataset.
Syntax: - PW=Password
Examples: -
Data ds6 (pw=sasadm);
Infile datalines;
Input id name$ sex$ age sal;
Datalines;
001 abc m 23 45000
002 def f 34 67000
003 mno m 21 36000
004 xyz f 27 45000
;
Run;
Data ds6b (pw=sasadm);
Infile "C:\Documents and Settings\stansys\Desktop\sample.txt";
Input id name$ sex$ age sal;
Run;
Proc import data file="C:\Documents and Settings\stansys\Desktop\sample.csv"
Out=work.ds(pw=krishna)
dbms=csv
replace;
Run;
Password should max up to 8 characters length it should contains Alphabetic,
under square and numbers but not any special characters, and numbers also not first.
Label: -
To assign the label name to data set.
Syntax: Label=Name
27
Examples: -
Page
Debug:-
Enables you to debug your program interactively by helping to identify logic
errors, and sometimes data errors
It will give two more windows after EDITOR Window like Proc DEBUGGER LOG and Proc
DEBUGGER SOURCE
Proc Debugger log contains information of where the data is not reading and Proc
Debugger source contain source code.
Syntax: Data dataset/Debug
28
PGM:-
__________________________________________________________________________________________
STANSYS SOFTWARE SOLUTIONS
#7-1-621/113(67/3RT), Beside: Nagarjuna High School, Near: S.R.Nagar Community Hall,
S.R.Nagar, Hyd-38|Ph:9542195422/8125980000|www.stansys.co.in|sas@stansys.co.in
Storing and Executing a Compiled Program
Syntax: Data dataset/pgm=dataset
Example: -
Data ds10 / pgm=work.ds1;
Infile datalines;
Input id name$ sex$ age sal;
Datalines;
001 abc m 23 45000
002 def f 34 67000
003 mno m 21 36000
004 xyz f 27 45000
;
Run;
The DATA step produces a stored compiled program named Work.DS1
Data pgm=WORK.DS1;
Run;
VIEW:-
Names a view that the DATA step uses to store the input DATA step view.
View-name must match one of the data set names
Syntax: Data dataset/view=name_of_the_view
Examples: -
Data ds11/ view=ds11;
Set sashelp.class;
Run;
Data ds12/ view=ds12 (source=save);
Set sashelp.class;
Run;
Data ds13/ view=ds13 (source=encrypt);
Set sashelp.class;
Run;
WARNING: An ALTER / PROTECT password is strongly recommended
for DATA STEP stored programs and views with encrypted source lines.
Set sashelp.class;
Run;
Page
A SAS view is a type of SAS data set that retrieves data values from other files. A SAS
view contains only descriptor information such as the data types and lengths of the
variables (columns), plus information that is required for retrieving data values from
other SAS data sets or from files that are stored in other software vendors' file formats.
SAS views are of member type VIEW. In most cases, you can use a SAS view as if it
were a SAS data file.
• Instead of using multiple DATA steps to merge SAS data sets by common
variables, you can construct a SAS view that performs a multi-table join.
__________________________________________________________________________________________
STANSYS SOFTWARE SOLUTIONS
#7-1-621/113(67/3RT), Beside: Nagarjuna High School, Near: S.R.Nagar Community Hall,
S.R.Nagar, Hyd-38|Ph:9542195422/8125980000|www.stansys.co.in|sas@stansys.co.in
• You can save disk space by storing a SAS view
definition, which stores only the instructions for where to find
the data and how it is formatted, not the actual data.
• SAS views can ensure that the input data sets are
always current because data is derived from SAS views at execution time.
SOURCE=source-option
Specifies one of the following source options:
SAVE: Saves the source code that created a stored compiled DATA step program or a
DATA step view.
ENCRYPT: Encrypts and saves the source code that created a stored compiled DATA
step program or a DATA step view.
Tip: If you encrypt source code, use the ALTER password option as well. SAS issues a
warning message if you do not use ALTER.
NOSAVE: Does not save the source code.
Note: If you use the NOSAVE option for a DATA step view, the view cannot be migrated
or copied from one version of SAS to another version.
DESCRIBE statement in a DATA step view is useful to write a copy of the source
code to the SAS log.
Data view=work.ds11;
Describe;
Run;
The following example executes a stored compiled DATA step program. It uses
the DESCRIBE statement to write a copy of the source code to the SAS log.
Data ds15/pgm=dss15;
Set sashelp.class;
Run;
Data pgm=work.dss15;
Describe;
Execute;
Run;
A SAS data file is a type of SAS data set that contains both descriptor information about
the data and the data values. SAS views contain only descriptor information and
Page
instructions for retrieving data that is stored elsewhere. Once the data is retrieved by
SAS, it can be manipulated in a DATA step.
A data file is static; a SAS view is dynamic.
When you reference a data file in a later PROC step, you see the data values as they
were when the data file was created or last updated. When you reference a SAS view in
a PROC step, the view executes and provides you with an image of the data values as
they currently exist, not as they existed when the view was defined.
SAS data files can be created on tape or on any other storage medium.
__________________________________________________________________________________________
STANSYS SOFTWARE SOLUTIONS
#7-1-621/113(67/3RT), Beside: Nagarjuna High School, Near: S.R.Nagar Community Hall,
S.R.Nagar, Hyd-38|Ph:9542195422/8125980000|www.stansys.co.in|sas@stansys.co.in
SAS views cannot be stored on tape. Because of their dynamic
nature, SAS views must derive their information from data
files on random-access storage devices, such as disk drives.
SAS views cannot derive their information from files stored on
sequentially accessed storage devices, such as tape drives.
SAS views are read only.
You cannot write to a SAS view, but some SQL views can be updated.
SAS data files can have an audit trail.
The audit trail is an optional SAS file that logs modifications to a SAS data file. Each time
an observation is added, deleted, or updated, information is written to the audit trail
about who made the modification, what was modified, and when.
SAS data files can have generations.
Generations provide the ability to keep multiple copies of a SAS data file. The multiple
copies represent versions of the same data file, which is archived each time it is
replaced.
SAS data files can have integrity constraints.
When you update a SAS data file, you can ensure that the data conforms to certain
standards by using integrity constraints. With SAS views, you can assign integrity
constraints to the data files that the views reference.
SAS data files can be indexed.
Indexing might enable SAS to find data in a SAS data file more quickly. SAS views
cannot be indexed.
SAS data files can be encrypted.
Encryption provides an extra layer of security to physical files. SAS views cannot be
encrypted.
SAS data files can be compressed.
Compression makes it possible to store physical files in less space. SAS views cannot be
compressed. The following figure illustrates native and interface SAS data files and their
relationship to SAS views.
31Page
Data patdata;
Infile Datalines;
Input p_id trt_code$;
Datalines;
101 A
102 A
103 B
104 B
;
Run;
Proc sort Data=patdata;
By p_id;
Run;
Data adverse;
Infile Datalines;
Input p_id event$;
Datalines;
101 headaches
107 fever
103 fracture
109 nausea
;
Run;
Proc sort Data=adverse;
By p_id;
Run;
Data pat_adverse;
32
By p_id;
/*If a;*/
If a then output;
Put a b; *observe in log
Run;
This options works with SET, MERGE, UPDATE, or MODIFY statements. Not with Datalines.
_Null_
The data set name _null_ is reserved for a special purpose here no data set will be
create but the programming step executes.
__________________________________________________________________________________________
STANSYS SOFTWARE SOLUTIONS
#7-1-621/113(67/3RT), Beside: Nagarjuna High School, Near: S.R.Nagar Community Hall,
S.R.Nagar, Hyd-38|Ph:9542195422/8125980000|www.stansys.co.in|sas@stansys.co.in
Instead of dataset name we can use _null_.
By using this we can save space in SAS Server
DATA _NULL_;
INFILE DATALINES;
INPUT ID NAME$ SEX$ AGE SAL;
DATALINES;
001 ABC M 20 2500
002 DEF M 22 3000
003 XYZ F 21 5000
;
RUN;
Use Put statement to see the variable information in log
Put Statement:-
It will write information in SAS log. (or)
It will write information in an external file with file statement.
Syntax: Put Variable(S)
Put Variable1 Variable2 Variable3;
Put Variable1= Variable2= Variable3=;
Put @1 Variable1 @10 Variable2 @20 Variable3;
Put @1 Variable1= @10 Variable2= @20 Variable3=;
Put Variable1;
Put Variable2;
Put Variable3;
Put Variable1=;
Put Variable2=;
Put Variable3=;
DATA _NULL_;
INFILE DATALINES;
INPUT ID NAME$ SEX$ AGE SAL;
PUT ID NAME SEX AGE SAL;
/*PUT ID= NAME= SEX= AGE= SAL=;*/
/*PUT @1 ID @10 NAME @20 SEX @30 AGE @40SAL;*/
34
DATALINES;
001 ABC M 20 2500
Page
File Statement:-
It writes the information from SAS to external file.
Syntax: File file-specification <options>
__________________________________________________________________________________________
STANSYS SOFTWARE SOLUTIONS
#7-1-621/113(67/3RT), Beside: Nagarjuna High School, Near: S.R.Nagar Community Hall,
S.R.Nagar, Hyd-38|Ph:9542195422/8125980000|www.stansys.co.in|sas@stansys.co.in
DATA DS;
INFILE DATALINES;
INPUT ID NAME$ SEX$ AGE SAL;
DATALINES;
001 ABC M 20 2500
002 DEF M 22 3000
003 XYZ F 21 5000
;
RUN;
DATA _NULL_;
SET DS;
FILE "C:\Documents and Settings\Administrator\Desktop\sas\sample.rtf ";
PUT @1 ID @5 NAME @10 SEX @14 AGE @18 SAL;
RUN;
We can create reports in TXT or RTF format using _null_, File & Put statements.
See below example for null reporting
Data Health;
Infile Datalines;
Input idno 1-4 name $ 6-24 team $ strtwght endwght;
Loss=strtwght-endwght;
Datalines;
1023 David Shaw red 189 165
1049 Amelia Serrano yellow 145 124
1219 Alan Nance red 210 192
1246 Ravi Sinha yellow 194 177
1078 Ashley McKnight red 127 118
1221 Jim Brown yellow 220 .
1095 Susan Stewart blue 135 127
1157 Rose Collins green 155 141
1331 Jason Schock blue 187 172
1067 Kanoko Nagasaka green 135 122
1251 Richard Rose blue 181 166
1192 Charlene Armstrong yellow 152 139
35
Put;
Put;
Page
__________________________________________________________________________________________
STANSYS SOFTWARE SOLUTIONS
#7-1-621/113(67/3RT), Beside: Nagarjuna High School, Near: S.R.Nagar Community Hall,
S.R.Nagar, Hyd-38|Ph:9542195422/8125980000|www.stansys.co.in|sas@stansys.co.in
38
Page
__________________________________________________________________________________________
STANSYS SOFTWARE SOLUTIONS
#7-1-621/113(67/3RT), Beside: Nagarjuna High School, Near: S.R.Nagar Community Hall,
S.R.Nagar, Hyd-38|Ph:9542195422/8125980000|www.stansys.co.in|sas@stansys.co.in