Start Day 3;
Format Statement
The format statement associates formats with the variables in the SAS data step.
General form of the format statement is: FORMAT variable(s) format;
Where the format is in the following form:
format.
$ : for character format
format: format name (such as: date, comma, dollar etc.)
w: Total width including all characters
. Required delimiter
d Number of decimal places
Selected SAS formats:
Standard Numeric format: w.d
5.2 12.46
12.2 123456789.12
Standard Character format: $w.
$3. ABC
$8. PROGRAMS
Use comma in number: COMMAw.d
comma6.0 12,345
comma9.2 12,345.12
Currency, dollar DOLLARw.d
Dollar6.0 $2,345
Dollar10.2 $12,345.12
Date format DATEw.
date7. 16FEB03
date9. 16FEB2003
YYMMDDw.
yymmdd10. 2003-02-16
MMDDYY.
mmddyy10. 02/16/2003
yymmn6. 200701
monyy7. FEB2012
Use format in data step
data show_format;
number=1234.1;
format number comma7.1;
name = 'His name is Tom';
format name $15. ;
balance=123456.28;
format balance dollar11.2; * Try dollar10.2. Then try dollar3.2
to see w.d format message;
dt=today();
format dt mmddyy10.;
format dt date9.; * If format is redefined, the
last format will over write the early ones;
run;
proc print data= show_format;
run;
Creating user defined format: PROC FORMAT
Using PROC FORMAT, user can create user defined format.
General form of the proc format step;
PROC FORMAT;
VALUE format-name range1 = ‘label’
range2 = ‘label’
…;
rangen = ‘label’ ;
RUN;
Format-name:
• For V9, a numeric format name can be up to 32 characters in length; a character format name can be up to 31 characters in length.
• For character values, the 1st character has to be “$”, the 2nd character has to be letter or “_”.
• For numeric values, the 1st character has to be letter or “_”.
• The last character cannot be number.
• Cannot be a name of SAS format.
Label:
• Can be up to 32,768 (2^15) characters in length
• Usually enclosed in quotes, but is not required.
Range(s)
Can be single value
Range of values
• Example:
libname cdat '/courses/ddbf9765ba27fe300';
data cdat.random_number;
do i=1 to 100;
number=int(ranuni(1)*100);
if number GT greater than
< LT less than
>= GE greater then or equal to
0 then bal_flg=1;
Perform more than one action for one IF … THEN … condition
Example:
IF class = ‘ANIMAL’ THEN obj1 = ‘DOG’;
IF class = ‘ANIMAL’ THEN obj2 = ‘CAT’;
IF class = ‘ANIMAL’ THEN obj3 = ‘DUCK’;
IF class = ‘AUTO’ THEN obj1 = ‘CAR’;
IF class = ‘AUTO’ THEN obj2 = ‘TRUCK’;
IF class = ‘AUTO’ THEN obj3 = ‘SUV’;
Note: For each THEN statement, only one action can be done. If for one if condition, multiple tasks need to be done, multiple IF condition has to be repeated. This is obviously not efficient.
DO statement
Syntax: DO;
One or more SAS statements;
END;
The DO … END statement allows a group of SAS statement to executed as a unit.
Example:
IF class = 'ANIMAL' THEN
DO;
obj1 = 'DOG';
obj2 = 'CAT';
obj3 = 'DUCK';
END;
ELSE IF class = 'AUTO' THEN
DO;
obj1 = 'CAR';
obj2 = 'TRUCK';
obj3 = 'SUV';
END;
Note: For every “DO;” statement, there must be an “END;” followed.
DO, Iterative statement
Syntax: DO index_var = specification-1 ;
More SAS statements;
END;
Examples:
Do month = ‘JAN’, ‘FEB’, ‘MAR’, ‘APR’, ‘MAY’;
DO INDEX = 3, 5, 8, 9, 12;
DO i = 1 to 10;
DO i = 1 to last;
DO i = a to b+2;
DO i = 1 to 20 by 2;
DO i = 10 to 1 by –1;
Example: DO iterative
In the following example, the first data step create a data set that has only one variable, path_name, it’s value is full path and filenames. The second data step use DO loop and SCAN function to find the filename from the path_name variable, and put the filename in the LOG window.
data file_list;
input path_name $ 1-60;
cards;
c:\sas_class\classdata\stocks2.sas7bdat
c:\sas_class\doc\class_note.doc
c:\Program Files\SAS Institute\SAS\v8\autoexec.sas
;
run;
data aa(drop=i j temp);
length temp $60 filename $60;
set file_list;
do i=1 to 100;
temp=scan(path_name,i,'\');
if temp='' then
do;
j=i-1;
filename=scan(path_name,j,'\');
put filename;
*** i=100; * method 1;
leave; * method 2;
end;
end;
run;
SELECT STATEMENT;
Syntax
SELECT ;
WHEN-1 (when-expression-1 ) statement;
END;
Example:
data saletax;
input state $ sale;
cards;
PA 345530
PA 945432
DE 456567
DE 345089
DE 94378
FL 247893
FL 104738
CA 753408
CA 948578
TX 342566
TX 654677
;
run;
data temp;
set saletax;
length note $40;
select (state);
when ('PA') tax=sale*.03;
when ('DE') tax=sale*.07;
when ('FL') do; tax=sale*0.0;
Note='No State Sale Tax'; end;
when ('CA') tax=sale*.09;
when ('TX') do; tax=sale*0.0;
Note='No State Sale Tax'; end;
otherwise;
end;
run;
Comparisons between select statement and if…then statement: When there are many conditions, Select Statement is more convenient. If there are only a few conditions, If … Then is more convenient.
DO UNTIL(expression);
Executes statements in a DO loop repetitively until a condition is true.
Example: The following program perform the same task as the above program using DO iterative. Obviously, for this kind of task, DO UNTIL is more convenient.
data file_list;
input path_name $ 1-60;
cards;
c:\sas_class\classdata\stocks2.sas7bdat
c:\sas_class\doc\class_note.doc
c:\Program Files\SAS Institute\SAS\v8\autoexec.sas
;
run;
data aa(drop=i temp);
length temp $60 filename $60;
set file_list;
i=0;
do until (temp='');
i=i+1;
temp=scan(path_name,i,'\');
end;
i=i-1;
filename=scan(path_name,i,'\');
put filename;
run;
DO WHILE (expression);
Executes statements repetitively while a condition is true.
Example: The following program perform the same task as the above program.
data file_list;
input path_name $ 1-60;
cards;
c:\sas_class\classdata\stocks2.sas7bdat
c:\sas_class\doc\class_note.doc
c:\Program Files\SAS Institute\SAS\v8\autoexec.sas
;
run;
data aa(drop=i temp);
length temp $60 filename $60;
set file_list;
i=0;
do while (temp ne '');
i=i+1;
temp=scan(path_name,i,'\');
end;
i=i-1;
filename=scan(path_name,i,'\');
put filename;
run;
Another example of using DO statement with array:
Libname cdat ‘/courses/ddbf9765ba27fe300‘;
data bal;
set cdat.bal_12_mth;
array bal_(12);
do i=1 to 12;
bal_(i)=bal_(i) + bal_(i)*0.03;
end;
Total_Balance=sum(of bal_1 - bal_12);
run;
Note: Without output statement, within a do loop, all iterations work with variables in one observation. Only until RUN, the data step goes to the next observation.
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- 3 day diet menu printable
- 3 day heart patient diet
- 3 day juice cleanse recipes
- 3 day detox diet menu
- 3 day belly fat cleanse
- homemade 3 day juice cleanse
- 3 day diet printable version
- 3 day diet menu lose 10 pounds
- 3 day cleanse to lose weight
- american heart association 3 day diet menu
- homemade 3 day cleanse
- best 3 day juice cleanse