Stansys: Software Solutions
Stansys: Software Solutions
Data type
Length
Label
Informat
Formats
DATATYPE
Indicates variable values are Character or Numeric data type.
If you specify $ symbol after the variable that is character data otherwise numeric data.
If you won’t specify $ symbol for character variable system thinks that is a numeric
variable but contains character data so output dataset contains missing value as period.
Character variable will read both character and numeric data. But numeric variable will
read only numeric data.
In dataset character data is left aligned, Numeric data is right aligned.
Example:-
Data DS;
Infile datalines;
Input id name$ sex$ age sal;
Datalines;
001 Ronald m 23 50000
002 Clark f 22 34500
003 Roopa f 26 45000
;
Run;
LENGTH STATEMENT
Specifies the number of bytes for storing variable values.
We can assign length for variables.
Syntax: - LENGTH variable(s)<$>length
Examples:-
Data DS1;
Length name $17.;
Infile datalines;
Input id name$ sex$ age sal ;
79
Datalines;
001 Ronaldregon m 23 50000
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/7671076710|www.stansys.co.in|[email protected]
Length statement always should write before Input or before
Set statement or before variable. Otherwise when input
executes whatever the length is there(default is 8) that will
come into output.
LABEL STATEMENT
Assigns descriptive labels to variables.
Assign alias names to variables.
Label can’t change the column name permanently like rename
Syntax: - LABEL variable-1='label-1' . . . <variable-n='label-n'>;
LABEL variable-1=' ' . . . <variable-n=' '>;
Data DS1;
Infile datalines;
Input id name$ sex$ age sal;
Label name='Emp Name' sex='Gender' sal='Income';
Datalines;
001 Ronald m 23 50000
002 Clark f 22 34500
003 Roopa f 26 45000
;
Run;
Data DS2;
Infile datalines;
Label name='Emp Name' sex='Gender' sal='Income';
Input id name$ sex$ age sal;
Datalines;
001 Ronald m 23 50000
002 Clark f 22 34500
003 Roopa f 26 45000
;
Run;
If we write Label statement after input statement output dataset will be in input order
But if we write before input, output dataset order should be in label order then what ever
variable is not there in label those will come in input order
If we specify label is ' ' raw data variable name should come into output dataset.
Means below example Label name=' ' so output dataset contains existing variable is
80
name
Page
Data DS3;
Infile datalines;
Input id name$ sex$ age sal;
Label name=' ' sex='Gender' sal=' ';
Datalines;
001 Ronald m 23 50000
002 Clark f 22 34500
003 Roopa f 26 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/7671076710|www.stansys.co.in|[email protected]
INFORMAT STATEMENT
Informat is an instruction that SAS uses to read data
values into a variable.
Informats are usually specified in an input statement. If
coded with the informat statements, attach an informat to a variable for subsequent
input.
Informats can be user-written Informats also.
Syntax: - INFORMAT variable-1<informat-1> variable-N<informat-N>;
Categories of Informats:-
1. Character Informats
2. Numeric Informats
3. Date, Time, Datetime Informats
4. Column binary Informats
5. User defined Informats
Character Informats: -
Reads character data into character variables.
Syntax: - informatw.
Ex: - $
$10.
$20.
$Char10.
Examples:-
Data infmt6;
Input id name$ age sex$ sal;
Datalines;
001 David 23 m 50000
002 Amelia 32 f 25000
003 Alan 31 f 30000
004 Ravi 21 m 45000
005 Jim 35 f 28000
;
Run;
Data informat6a;
Input idno name & $18. team$ strtwght endwght;
Datalines;
81
Numeric Informats: -
Reads numeric data values from numeric variables
Syntax: - informatw.d
__________________________________________________________________________________________
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/7671076710|www.stansys.co.in|[email protected]
Ex: - Comma12. Dollar10.2
Examples:-
Data infmt1;
Infile datalines;
Input id name$ age sex$ sal;
Datalines;
001 David 23 m 50000
002 Amelia 32 f 25000
003 Alan 31 f 30000
004 Ravi 21 m 45000
005 Jim 35 f 28000
;
Run;
In above example data and columns are same data type so you can read. But see below program
sal values are containing special character but sal variable is numeric so you can’t read data in
this case we can specify informat to read data, not only with comma when numeric data contains
special characters like comma, dollar, euro we can specify Numeric Informats like below.
Data infmt2;
Infile datalines;
Input id name$ age sex$ sal comma6.;
Datalines;
001 David 23 m 50,000
002 Amelia 32 f 25,000
003 Alan 31 f 30,000
004 Ravi 21 m 45,000
005 Jim 35 f 28,000
;
Run
Data infmt3;
Infile datalines;
Input id name$ age sex$ sal;
Informat sal comma6. ;
Datalines;
001 David 23 m 50,000
002 Amelia 32 f 25,000
003 Alan 31 f 30,000
004 Ravi 21 m 45,000
005 Jim 35 f 28,000
82
Run;
Data infmt4;
Page
Infile datalines;
Input id name$ age sex$ sal dollar5. ;
Datalines;
001 David 23 m $5000
002 Amelia 32 f $2500
003 Alan 31 f $3000
004 Ravi 21 m $4500
005 Jim 35 f $28000
;
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/7671076710|www.stansys.co.in|[email protected]
Data infmt5;
Infile datalines; Informat we can
write with Input
Input id name$ age sex$ sal dollar6. ; statement after
the variable or we can
/*Informat sal dollar6. ;*/ write as a separate statement like this
Datalines;
001 David 23 m $5,000
002 Amelia 32 f $2,500
003 Alan 31 f $3,000
004 Ravi 21 m $4,500
005 Jim 35 f $2,800
;
Run;
Data infmt6;
Infile datalines;
Input id name$ age sex$ sal dollar9.2 ;
Format sal dollar10.2;
Datalines;
001 David 23 m $50000.55
002 Amelia 32 f $25000.00
003 Alan 31 f $30000.05
004 Ravi 21 m $45000.07
005 Jim 35 f $28000.99
;
Run;
Examples:-
Page
Data infmt7;
Infile datalines;
Input id name$ age sex$ sal dob;
Datalines;
001 David 23 m 50000 10Feb1983
002 Amelia 32 f 25000 15May1984
003 Alan 31 f 30000 21Jul1984
004 Ravi 21 m 45000 05Aug1984
005 Jim 35 f 28000 30Jan1985
;
__________________________________________________________________________________________
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/7671076710|www.stansys.co.in|[email protected]
Run;
In SAS dates are Numeric data type but DOB values contains
character values in above example, so we can’t read, to read
dates we should use date informats like below
Data infmt7a;
Infile datalines;
Input id name$ age sex$ sal dob date9.;
/*Informat dob date9.;*/
Datalines;
001 David 23 m 50000 10Feb1983
002 Amelia 32 f 25000 15May1984
003 Alan 31 f 30000 21Jul1984
004 Ravi 21 m 45000 05Aug1984
005 Jim 35 f 28000 30Jan1985
;
Run;
Data infmt7b;
Infile datalines;
Input id name$ age sex$ sal dob date7.;
/*Informat dob date7.;*/
Datalines;
001 David 23 m 50000 10Feb83
002 Amelia 32 f 25000 15May84
003 Alan 31 f 30000 21Jul84
004 Ravi 21 m 45000 05Aug84
005 Jim 35 f 28000 30Jan85
;
Run;
Data infmt7c;
Infile datalines;
Input id name$ age sex$ sal dob anydtdte.;
/*Informat dob date9.;*/
Datalines;
001 David 23 m 50000 10Feb1983
002 Amelia 32 f 25000 15May1984
003 Alan 31 f 30000 21Jul1984
004 Ravi 21 m 45000 05Aug1984
005 Jim 35 f 28000 30Jan1985
84
;
Run;
Page
Data infmt8;
Infile datalines;
Input id name$ age sex$ sal dob date9. doj:ddmmyy10. ;
/*Input id name$ age sex$ sal dob anydtdte9. doj:anydtdte10.;*/
Datalines;
001 David 23 m 50000 10Feb1983 12/01/2011
002 Amelia 32 f 25000 15May1984 15/01/2011
003 Alan 31 f 30000 21Jul1984 31/01/2011
004 Ravi 21 m 45000 05Aug1984 25/02/2011
__________________________________________________________________________________________
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/7671076710|www.stansys.co.in|[email protected]
005 Jim 35 f 28000 30Jan1985 08/03/2011
;
Run;
Data infmt8a;
Infile datalines;
Input id name$ age sex$ sal dob date9. doj:ddmmyy8. ;
/*Informat dob date9. doj ddmmyy8.;*/
Datalines;
001 David 23 m 50000 10Feb1983 12/01/11
002 Amelia 32 f 25000 15May1984 15/01/11
003 Alan 31 f 30000 21Jul1984 31/01/11
004 Ravi 21 m 45000 05Aug1984 25/02/11
005 Jim 35 f 28000 30Jan1985 08/03/11
;
Run;
Data infmt8b;
Infile datalines;
Input id name$ age sex$ sal dob doj;
Informat dob date9. doj ddmmyy10.;
Datalines;
001 David 23 m 50000 10Feb1983 12/01/2011
002 Amelia 32 f 25000 15May1984 15/01/2011
003 Alan 31 f 30000 21Jul1984 31/01/2011
004 Ravi 21 m 45000 05Aug1984 25/02/2011
005 Jim 35 f 28000 30Jan1985 08/03/2011
;
Run;
Data infmt9;
Infile datalines;
Input id name$ age sex$ sal dob datetime.;
Datalines;
001 David 23 m 50000 10Feb1983:10:30:15
002 Amelia 32 f 25000 15May1984:11:23:23
003 Alan 31 f 30000 21Jul1984:08:34:45
004 Ravi 21 m 45000 05Aug1984:12:43:56
005 Jim 35 f 28000 30Jan1985:03:35:12
;
Run;
85
Data infmt9a;
Input id name$ age sex$ sal dob ;
Page
FORMAT STATEMENT
Format is an instruction that SAS uses to write data values.
The format is exactly the same as that for informat.
Infact most SAS defined informats are also SAS defined formats.
However there are some informats such as anydtdte.
That is not defined as Format
Syntax: - FORMAT variable-1<format-1> variable-N<format-N>;
86
Categories of Formats:-
Page
1. Character Formats
2. Numeric Formats
3. Date, Time, Datetime Formats
4. Column binary Formats
5. User defined Formats
Character Formats: -
Writes character data values from character variables
__________________________________________________________________________________________
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/7671076710|www.stansys.co.in|[email protected]
Character informats and character formats both are same
Syntax: - $ formatw.
Ex: - $
$10.
$20.
$char10.
Examples:-
Data fmt5;
Infile datalines;
Input id name$ age sex$ sal;
Datalines;
001 David 23 m 50000
002 Amelia 32 f 25000
003 Alan 31 f 30000
004 Ravi 21 m 45000
005 Jim 35 f 28000
;
Run;
Data fmt5a;
Infile datalines;
Input idno name & $18. team$ 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 .
;
Run;
Numeric Formats: -
Writes numeric data values from numeric variables.
Syntax: - formatw.d
Ex: - Dollar10.
Dollar 10.2
87
Comma10.
Comma10.2
Page
Percent 4.2
Best10.
Examples:-
Data fmt1;
Infile datalines;
Input id name$ age sex$ sal comma6.;
Format sal comma6.;
/*Format sal comma9.2;*/
Datalines;
__________________________________________________________________________________________
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/7671076710|www.stansys.co.in|[email protected]
001 David 23 m 50,000
002 Amelia 32 f 25,000
003 Alan 31 f 30,000
004 Ravi 21 m 45,000
005 Jim 35 f 28,000
;
Run;
Data fmt2;
Infile datalines;
Input id name$ age sex$ sal;
Informat sal comma9.2;
Format sal comma9.2;
Datalines;
001 David 23 m 50,000.55
002 Amelia 32 f 25,000.00
003 Alan 31 f 30,000.60
004 Ravi 21 m 45,000.77
005 Jim 35 f 28,000.50
;
Run;
Data fmt3;
Infile datalines;
Input id name$ age sex$ sal dollar5. ;
Format sal dollar5.;
Datalines;
001 David 23 m $5000
002 Amelia 32 f $2500
003 Alan 31 f $3000
004 Ravi 21 m $4500
005 Jim 35 f $28000
;
Run;
Data fmt3a;
Infile datalines;
Input id name$ age sex$ sal dollar6. ;
/*Format sal dollar6.;*/
Format sal dollar9.2;
/*Format sal comma6.;*/
88
Datalines;
001 David 23 m $5,000
Page
29Jan2010
ddmmyy8.
29/01/10
ddmmyy10.
29/01/2010
time5. 10:30
time8. 10:30:20
datetime. 29Jan10:10:30:20
datetime20. 29Jan2010:10:30:20
worddate20. January 29, 2010
weekdate30. Friday, January 29,2010
yymmddn8. 20100129
yymmddd8. 10-01-29
yymmddd10. 2010-01-29
yymmdds8. 10/01/29
yymmdds10. 2010/01/29
yymmddc8 10:01:29
Yymmddc10. 2010:01:29
Examples:-
Data fmt6;
Infile datalines;
Input id name$ age sex$ sal dob date9.;
Format dob date9.;
/*Format dob date7.;*/
/*Format dob date9.;*/
/*Format dob ddmmyy8.;*/
/*Format dob ddmmyy10.;*/
/*Format dob worddate20.;*/
/*Format dob weekdate30.;*/
89
__________________________________________________________________________________________
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/7671076710|www.stansys.co.in|[email protected]
004 Ravi 21 m 45000 05Aug1984
005 Jim 35 f 28000 30Jan1985
;
Run;
Data fmt7;
Infile datalines;
Input id name$ age sex$ sal dob date9. doj:ddmmyy10. ;
Format dob worddate20. doj weekdate30.;
Datalines;
001 David 23 m 50000 10Feb1983 12/01/2011
002 Amelia 32 f 25000 15May1984 15/01/2011
003 Alan 31 f 30000 21Jul1984 31/01/2011
004 Ravi 21 m 45000 05Aug1984 25/02/2011
005 Jim 35 f 28000 30Jan1985 08/03/2011
;
Run;
Data fmt8;
Infile datalines;
Input id name$ age sex$ sal dob ;
Informat dob datetime.;
Format dob datetime.;
/*Format dob datetime20.;*/
Datalines;
001 David 23 m 50000 10Feb1983:10:30:15
002 Amelia 32 f 25000 15May1984:11:23:23
003 Alan 31 f 30000 21Jul1984:08:34:45
004 Ravi 21 m 45000 05Aug1984:12:43:56
005 Jim 35 f 28000 30Jan1985:03:35:12
;
Run;
Data fmt9;
Infile datalines;
Input id name$ age sex$ sal dob ;
Informat dob time.;
Format dob time.;
/*Format dob time8.;*/
/*Format dob time5.;*/
90
Datalines;
001 David 23 m 50000 10:30:15
Page
Note: we can specify attributes as individually with above statements like Length, Label, Informat
and Format or all attributes we can specify with one statement that is Attrib Statement.
ATTRIB STATEMENT
Associates a format, informat, label, and/or length with one or more variables
Syntax: - ATTRIB variable-list(s) attribute-list(s) ;
Generally using Attrib statement we can change length, format, informat and label.
Data DS1;
Attrib name length=$10.;
Input id name$ sex$ age sal ;
Datalines;
001 Ronald m 23 50000
002 Clark f 22 34500
003 Roopa f 26 45000
;
Run;
Data DS2;
Attrib name length=$10. label='Emp Name';
Input id name$ sex$ age sal;
Datalines;
001 Ronald m 23 50000
002 Clark f 22 34500
003 Roopa f 26 45000
;
Run;
Data DS3;
Attrib name length=$10. label='Emp name'
sal format=comma6. label='Income';
Input id name$ sex$ age sal;
Datalines;
001 Ronald m 23 50000
002 Clark f 22 34500
003 Roopa f 26 4500
92
;
Run;
Page
Data DS4;
Attrib name length=$10.
doj format=worddate30. Label='Date of joining'
sex label='gender' sal format=dollar7. ;
Input id name$ sex$ age sal doj date9.;
Datalines;
001 Ronald m 23 50000 02Jan2009
002 Clark f 22 34500 22Feb2010
003 Roopa f 26 45000 30Apr2010
;
__________________________________________________________________________________________
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/7671076710|www.stansys.co.in|[email protected]
Run;
Data DS5;
Attrib name length=$10.
dob informat=date9. format=ddmmyy8.
Label='Date of Birth'
doj informat=anydtdte9. format=ddmmyy10. Label='Date of Joining'
sex label='gender'
sal format=dollar7. ;
Infile datalines;
Input id name$ sex$ age sal dob doj ;
Datalines;
001 Ronald m 23 50000 11Mar1986 02Jan2009
002 Clark f 22 34500 30Dec1986 22Feb2010
003 Roopa f 26 45000 06Aug1987 30Apr2010
;
Run;
Data DS6;
Infile datalines;
Input id name$ sex$ age sal dob doj ;
Attrib name label='Empname'
dob informat=date9. format=ddmmyy8. Label='Date of Birth'
doj informat=anydtdte9. format=ddmmyy10. Label='Date of Joining'
sex label='gender'
sal format=dollar7. ;
Datalines;
001 Ronald m 23 50000 11Mar1986 02Jan2009
002 Clark f 22 34500 30Dec1986 22Feb2010
003 Roopa f 26 45000 06Aug1987 30Apr2010
;
Run;
Data DS7;
Infile datalines;
Input id name$ sex$ age sal dob date9. doj:date9. ;
Datalines;
001 Ronald m 23 50000 11Mar1986 02Jan2009
002 Clark f 22 34500 30Dec1986 22Feb2010
003 Roopa f 26 45000 06Aug1987 30Apr2010
;
93
Run;
Page
Data DS7a;
Attrib name length=$10.
dob informat=date9. format=ddmmyy8. Label='Date of Birth'
doj informat=anydtdte9. format=ddmmyy10. Label='Date of Joining'
sex label='gender'
sal format=dollar7. ;
Set DS6;
Run;
Note: When we specify length as an attribute with Attrib statement, Attrib statement
must write before Input or before Set statement otherwise when input statement or set
__________________________________________________________________________________________
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/7671076710|www.stansys.co.in|[email protected]
statement executes whatever the length is there for variable
that will come into output.
And the output dataset variable order also change accordingly attrib statement variables
order that we can change again into required order in Proc print
But if we specify Attrib statement after Input or Set statement dataset order is Input
statement order (we can specify like this when we are not using Length in attrib
statement).
Interview Questions
Q1) What is INFORMAT? Syntax?
Q2) What is FORMAT? Syntax?
Q3) Write any three Informats & Formats with examples?
Q4) What is user defined formats? How can you create it?
Q5) Difference between Informats & Formats?
Q6) Explain about character Informats and Formats?
Q7) What is difference between $w. and $charw. ?
Q6) Write about below numeric informats & Formats? Write examples?
a) Commaw.d
b) Dollarw.d
c) Percentw.d
d) Bestw.d
Q7) Write about below Date informats & Formats values with current system date?
a) Date7.
b) Date9.
c) Ddmmyy8.
d) Ddmmyy10.
e) Anydtdte.
f) Datetime.
g) Time.
h) Datetime20.
i) Worddate20.
j) Weekdate30.
k) Yymmddn8.
94
l) Yymmddc8.
m) Yymmddd8.
Page
n) Yymmdds8.
o) Yymmddd10.
p) Yymmddc10.
q) Yymmdds10.
r) Mmddyys10.
s) Mmddyys10.
t) Mmddyyc10.
Q8) How dates work in SAS?
Q9) I have a variable called DOB and the values are in Date7 format.
__________________________________________________________________________________________
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/7671076710|www.stansys.co.in|[email protected]
Iam reading that value into SAS without specifying any
informat so what is the
output? If i use with informat what is the output?
Q10) I have a variable called DOB and the values are in
Datetime.
Iam reading that value into SAS without specifying any informat so what is the
output? If i use with informat what is the output?
Q11) I have a variable called BIRTHTIME and the values are in time8.
Iam reading that value into SAS without specifying any informat so what is the
output? If i use with informat what is the output?
Q12) While reading data when i use informats for DATE Variables, values are coming as
a numbers but i want as dates what should i do?
Q13) I have a DATE Value 10Feb1959 I am reading that data into SAS using informat,
What is the value in output dataset?
Q14) I have a DATE Value 10Feb2012 I am reading that data into SAS using informat,
What is the value in output dataset?
Input statement?
Q26) Data is available in the location of "E:\SAS\RAW_DATA\IQ\LENGTH"
Tasks are available at TASK notepad in above same location? Read that data into
SAS and write the program for each task ?
__________________________________________________________________________________________
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/7671076710|www.stansys.co.in|[email protected]
Q31) Data is available in the location of
"E:\SAS\RAW_DATA\IQ\ATTRIB"
Tasks are available at TASK notepad in above same
location? Read that data into
SAS and write the program for each task ?
96
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/7671076710|www.stansys.co.in|[email protected]
97
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/7671076710|www.stansys.co.in|[email protected]