Professional Documents
Culture Documents
Agenda
Topics to be covered
SAS Informats
Numeric Informats
Character Informats
Date Informats
SAS Formats
Numeric Formats
Character Formats
Date Formats
Best place to work
SAS Informats & Formats Contd…. Best place to bank
DATA TEST1;
Numeric Informats INPUT
AMT1 : 8.2 /* Tot length 8 digits with2 decimal places */
1. width. (8. 15. 32. ) AMT2 : COMMA8.2 /* To account for $ and embedded comma */
AMT3 : COMMAX8.2 /* Comma and period interchanging places */
2. width.decimal (8.2 15.2 32.5) PER1 : PERCENT4.2 /* To account for % symbol */
AMT4 : NUMX8.2 /* Decimal point replaced by comma */
3. Commawidth.decimal (comma32.2) ;
DATALINES;
4. CommaXwidth.decimal (commaX15.2) 1236.00 $1,000.00 $700.000,00 5.6% 850,00
1234.00 $2,500.00 $600.700,00 6.89% 680,78
5. NUMXwidth.decimal (NUMX8.2) 1236.78 $343.00 $800.800,00 17.56% 780,67
;
6. PERCENTwidth.decimal (PERCENT2.1) RUN;
4
Best place to work
Character Informats
DATA TEST2;
1. $width. ($50.) INPUT
STR1 : $10. /* Length of the string is 10 characters */
2. $upcasewidth. ($upcase3. ) STR2 : $UPCASE8. /* Up-casing the string */
STR3 : $REVERS5. /* Reading string in reverse order & trimming leading/trailing blanks */
3. $reverswidth. ($revers7. ) STR4 : $REVERJ5. /* Reading string in reverse order but NOT trimming blanks*/
;
4. $reverjwidth. ($reverj9. ) DATALINES;
Summary summary summary summary
;
RUN;
5
Best place to work
DATA TEST2;
Date Informats: Part 1 INPUT
DATE1 : DATE9.
DATE2 : DDMMYY10. /* Notice the date separator “/” */
1. DATE9. (17MAR2010) DATE3 : DDMMYY10. /* Notice the date separator “:” */
DATE4 : DDMMYY10. /* Notice the date separator “-” */
2. DDMMYY10. (17/03/2001) DATE5 : DDMMYY10. /* Notice the date separator “.” */
DATE6 : JULIAN7.
3. JULIAN7. (2001077) ;
Other Variations DATALINES;
17MAR2001 17/03/2001 17:03:2001 17-03-2001 17.03.2001 2001077
1. MMDDYY10. 16MAR1985 16/03/1985 16:03:1985 16-03-1985 16.03.1985 1985076
;
2. YYMMDD10.
RUN;
3. YYDDMM10.
6
Best place to work
DATA TEST3;
Date Informats: Part 2 INPUT
DATE1 : DATE7.
1. DATE7. (17MAR01) DATE2 : DDMMYY8. /* Date Separators can be “-” “.” “:” */
DATE3 : DATE11.
2. DATE11. (17-MAR-2001) DATE4 : MONYY5. /* Date value that corresponds to the 1 st day of the month */
DATE5 : YYMMN4. /* No separator permissible and day is 1st day of the month */
3. DDMMYY8.(17/03/01) DATE6 : JULIAN5.
;
4. MONYY5. (APR01) DATALINES;
17MAR01 17/03/01 17-MAR-2001 MAR01 0103 01077
5. JULIAN5.. (01077) 16MAR85 16/03/85 16-MAR-1985 MAR85 8503 85076
;
6. YYMMN4. (0103) RUN;
Other Variations
1. MONYY7.
2. YYMMN6.
3. DDMMYY6.
Demonstration of Date Informat Part 2
4. DDMMYY8. (No date
separator)
Obs DATE1 DATE2 DATE3 DATE4 DATE5 DATE6
5. WEEKVW. (YYY-Www-dd,
Year, week, day of week) 1 15051 15051 15051 15035 15035 15052
2 9206 9206 9206 9191 9191 9207
6. YYQ4. (85Q1)
7
Best place to work
DATE FORMATS
8
Best place to work
DATE FORMATS
Few More!!!
1. WORDDATEw.
2. WORDDATXw.
3. YEARw.
4. YYQRw.
9
Best place to work
String Manipulations
12
Best place to work
Example
Data phone;
Input phone $15.;
Number = compress(phone,,'kd');
Datalines;
(908)235-4490
(201) 555-77 99
;
phone Number
(908)235-4490 9082354490
(201) 555-77 99 2015557799
13
Best place to work
14
Best place to work
Address=tranwrd(address,’Street’,’St.’);
Address=tranwrd(address,’Avenue’,’Ave.’);
Address=tranwrd(address,’Road’,’Rd.’);
Before After
15
Best place to work
Before After
NOTE: IFC & IFN have similar syntax. If vtype is numeric then we use IFN.
16
Best place to work
expression 1 or more
times
characters
17
Best place to work
Pattern Matching
18
Best place to work
12345678 02-9428044
1234 2-12345678
1234-45 02-1234
Data Work.PERL;
Input Id $ Phone $11. ;
Match1=Prxmatch("/\d{8}/",Id);
Match2=Prxmatch("/\d{2}(-\d{8})/",phone);
Datalines;
12345678 02-94280444 Match1 Match2
1234 2-12345678 1 1
; 0 0
Run; 0 0
19
Best place to work
Advantage of using PRXCHANGE over TRANWRD is that you can IF _N_ = 1 THEN MATCH = PRXPARSE("s/[Cc]at/Mouse/");
search for strings using wild cards. *Replace "Cat" or "cat" with Mouse;
Retain MATCH;
Syntax: CALL PRXCHANGE(pattern-id or regular-expression, times, CALL PRXCHANGE(MATCH,-1, TEXT, NEW_TEXT, R_LENGTH, TRUNC,
N_OF_CHANGES);
old-string <, new-string <, result-length <, truncation-value <,
number-of-changes>>>>); IF TRUNC THEN PUT "Note: NEW_TEXT was truncated";
DATALINES;
The Cat in the hat
There are two cat cats in this line
;
PROC PRINT DATA=CAT_AND_MOUSE NOOBS;
String Function
TITLE "Listing of CAT_AND_MOUSE";
repository RUN;
20
Best place to work
Arrays
Data Work.Arraydata;
DIM Input key 3. opened date9. purchcm01
purchcm02 purchcm03 purchcm04
The DIM function returns the number of literal elements in an array. purchcm05 purchcm06 5.;
Datalines;
It functions against multi-dimensional arrays as well as one-dimensional arrays. 1 01jan2009 100 150 350 700 800 850
2 15jan2009 0 0 100 1000 2000 2500
1-dimensional array example 3 20jan2009 50 100 200 400 900 1500
4 05feb2009 500 500 1000 2000 2500
DIM(array_name) 5000
;
Multi-dimensional array examples Run;
DIM(m_array) -> returns the number of elements in the first dimension of the array %MACRO COUNT(DSN);
DATA _NULL_;
DIM5(m_array) -> returns the number of elements in the 5th dimension of the array IF 0 THEN SET &DSN;
DIM(m_array, 5) -> returns the number of elements in the 5th dimension of the array ARRAY NUMS[*] _NUMERIC_;
ARRAY CHARS[*] _CHARACTER_;
N_NUMS = DIM(NUMS);
Data Arraydata; N_CHARS = DIM(CHARS);
Input key 3. opened date9. purchcm01 There are 8 numeric PUT / "There are " N_NUMS "numeric
Variables and0 Character Variables and" N_CHARS
purchcm02 purchcm03 purchcm04 Variables "Character Variables";
RUN;
purchcm05 purchcm06 5.; %MEND COUNT;
%COUNT(Work.Arraydata);
21
Best place to work
Array Implementation
Key opendate n_cob pur
DATA Arraydemo;
/* Defining arrays for variables used */
1 01jan2009 1 100
1 01jan2009 2 50
Array purch {6} purchcm01-purchcm06; 1 01jan2009 3 200
Set Arraydata; 1 01jan2009 4 350
1 01jan2009 5 100
DO I=1 TO 6; 1 01jan2009 6 50
n_cob=1; 2 15jan2009 1 0
IF purch {I} = . 2 15jan2009 2 0
THEN purch {I}=0; Key opendate pur1 pur2 pur3 pur4 pur5 pur6 2 15jan2009 3 100
2 15jan2009 4 900
IF (I>1) THEN DO;
2 15jan2009 5 1000
J = I-1; 1 01jan2009 100 150 350 700 800 850 2 15jan2009 6 500
purchase = SUM(purch {I}, -1 * purch {J}); 2 15jan2009 0 0 100 1000 2000 2500 3 20jan2009 1 50
End; 3 20jan2009 50 100 200 400 900 1500 3 20jan2009 2 50
Else purchase = purch {I}; 4 05feb2009 500 500 1000 2000 2500 5000 3 20jan2009 3 100
output; 3 20jan2009 4 200
END; 3 20jan2009 5 500
3 20jan2009 6 600
Run;
4 05feb2009 1 500
4 05feb2009 2 0
4 05feb2009 3 500
4 05feb2009 4 1000
4 05feb2009 5 500
4 05feb2009 6 2500
22
Best place to work
23
Best place to work
Date Arithmetic
* Simple arithmetic functions with days;
data _null_;
date = date();
my_birthday = '23jan63'd;
datedif2 = intck('month',my_birthday,date); * intck(‘interval’, from, to );
datedif3 = sum(date,-my_birthday); * sum(to ,-from );
datedif1 = datdif(my_birthday,date,'act/act'); * datdif(from,to ,’act/act’) or ‘30/360’;
put ‘Log shows: ‘ datedif1 datedif2 datedif3;
run;
Log shows: 538 16357 16357
data _null_;
my_birthday = '23jan1963'd;
years = intck('year',my_birthday,date());
quarters = intck('qtr',my_birthday,date());
months = intck('month',my_birthday,date());
weeks = intck('week',my_birthday,date());
days = intck('day',my_birthday,date());
put ‘Log shows: ‘ years quarters months weeks days;
run;
Log shows: 44 176 529 2299 16092
24
Best place to work
date2=intnx('month','01jan95'd,5,'middle'); 12949
put date2 / date2 date7.; 15JUN95
date3=intnx('month','01jan95'd,5,'end'); 12964
put date3 / date3 date7.; 30JUN95
date4=intnx('month','01jan95'd,5,'sameday'); 12935
put date4 / date4 date7.; 01JUN95
date5=intnx('month','15mar2000'd,5,'same'); 14837
put date5 / date5 date9.; 15AUG2000
interval='month';
date='1sep2001'd;
align='m';
date4=intnx(interval,date,2,align); 15294
put date4 / date4 date7.; 15NOV01
x1='month ';
x2=trim(x1);
date='1sep2001'd + 90;
date5=intnx(x2,date,2,'m'); 15356
put date5 / date5 date7.; 16JAN02
25
Best place to work
Some useful date formats & Informats Best place to bank
26
Best place to work
27
Best place to work
28
Best place to work
How to create?
An Index is a physical file structure Types
1. PROC DATASETS
which serves as an adjunct to a 1. Simple Index
SAS data set principally containing 2. PROC SQL
INDEX variable and RECORD ID. 2. Composite Index
3. Data step
PROC SQL;
SAS Institute Recommendation CREATE UNIQUE INDEX ID ON WORK.ID1
QUIT;
1. Index var have values which are frequently used in WHERE clause
2. Less than 25% have same value of Index variable
3. Uniform distribution of values of Index variable DATA TEST(INDEX=(ID /UNIQUE /NOMISS));
SET ID1;
RUN;
SAS Indexes
29
Best place to work
SAS: PROC IMPORT Best place to bank
run;
31
Best place to work
Proc Datasets
32
Best place to work
Basics
Proc format;
value BPInterp low-<120="Normal"
120-<140="Prehypertension" Interpretation N PctN
140-<160= "Stage 1 Hypertension" Normal 106 21.20
160-high="Stage 2 Hypertension" Prehypertension 189 37.80
;
Stage 1 Hypertension 169 33.80
run;
Stage 2 Hypertension 36 7.20
proc tabulate data=bp ; Total 500 100.00
class bp;
tables bp="" all="Total",(N
pctn)/box="Interpretation";
format bp BPInterp.;
run; data bp;
set bp;
length Interpret $21;
if bp < 120 then Interpret="Normal";
Advantage:
else if 120 <= bp < 140 then
HELPS
Interpret="Prehypertension";
Proc format REPLACE
else if 140 <= bp < 160 then
CODE LIKE:
Interpret="Stage 1 Hypertension";
else Interpret="Stage 2
Hypertension";
run;
Best place to work
Picture formats
34
Best place to work
Picture format
35
Best place to work
data test;
input date : date9.;
datalines;
19SEP1999
23OCT1983
01SEP1980
01SEP2010
;
run;
proc format;
picture dispdate
low -< 10000 = [date9.]
10000 -< 15000 = [mmddyy8.]
15000 - high = "99,9999"
;
36
Best place to work
Creates Format
Dumping to a dataset
proc format library=formats cntlout = fmt_dump;
select
pinstate;
run;
Uses Format
37
Best place to work
Format Lookup
Format Lookup
38
Best place to work
39
Best place to work
40
Best place to work
ACROSS
41
Best place to work
NESTING VARIABLES
42
Best place to work
Proc report -
Compute
43
Best place to work
Adding Images to
SAS reports
ExcelXp_attr_demo
ExcelXp_sample
44
Best place to work
45
Best place to work
46
Best place to work
2D - 1
2D - 2
47
Best place to work
48
Best place to work
Multisheet Sample
ExcelXp
49
Best place to work
ODS DESTINATIONS
proc report,
proc freq
Destination Result
SAS
Data Component Output Data Set
Output
Output Listing Window
Object
HTML
HTML File
Template
Components RTF
RTF File
style template,
table template
Best place to work
Table and Style templates are the two main Many table templates are shipped with SAS and they are used for
Table templates provide instructions for Table templates control data column order, column headers and
Style templates provide presentation A table template can be bound to an output object in a data step.
51
Best place to work
52
Best place to work
“Safe” Fonts
“Safe” fonts1,2
Times New Roman, Times
Arial, Helvetica
Arial Black, Arial, Helvetica
Book Antigua, Times New Roman, Times
Courier New, Courier
Comic Sans MS, Arial, Helvetica
Verdana, Arial, Helvetica
Impact, Arial Black, Helvetica
Georgia, Times New Roman, Times
News Gothic MT, Arial, Helvetica
Tahoma, Arial, Helvetica
Trebuchet MS, Arial, Helvetica
Enlists many different predefined styles and other useful pointers on Styles
HTML_Styles
http://support.sas.com/rnd/base/topics/statgraph/v91StatGraphStyles.htm#Reference
http://support.sas.com/resources/papers/proceedings10/031-2010.pdf
53
Best place to work
define main;
text "Alphabetic List of Variables and Attributes";
space = 1;
spill_adj;
spill_margin;
end;
define Num;
header="/#/";
style=RowHeader;
id;
end;
define Variable;
header="Variable";
width_max=name_width_max;
width=name_width;
end;
define Type;
header="Type";
end;
define Len;
header="Len";
end;
... content snipped ...
col_space_max = 4;
col_space_min = 1;
newpage=off
balance;
end;
54
Best place to work
55
Best place to work
Advanced topics
define table mytable;
column name sex (age height weight);
define age;
• Adding new variables using compute define header ageheightweight;
text 'Age*Height*Weight';
define table mytable;
column name sex age height weight split = '*';
bmi; end;
define bmi; header = ageheightweight;
compute as (weight * 703) / (height * format = 3.;
height); end;
header = 'BMI'; define height;
format = 2.; format = 3.;
end; end;
end;
define weight;
format = 3.;
• Controlling style attributes end;
end;
define table mytable;
column name sex age height weight;
define height;
style = { font_weight=bold background=red foreground=white };
end; Reference:
define weight;
http://support.sas.com/rnd/base/ods/tem
style = { font_weight=bold background=red foreground=white }; plateFAQ/index.html
end; BLANK_DUPS
end; specifies that only the first occurrence of consecutive same
values should appear in a column.
JUST=
• Controlling column attributes: specifies the horizontal alignment of the cell content. The value
can be LEFT, RIGHT, or CENTER.
PRINT_HEADERS
specifies whether or not to print the column header.
VJUST=
specifies the vertical alignment of the cell content. The value
can be TOP, CENTER, or BOTTOM.
57
Best place to work
MACROS
•A tool to customize SAS system
•Used to generate (often repeated or conditional) SAS code
•Modularizing code into reusable / portable program units
•Conditional compilation and execution of codes
OPTIMIZATION
•CPU Time
•Memory
•Data Storage
•I/O
AUTOMATION
•Library
•Date Manipulation
•Dataset Exists / Observations Count
•Scanning Log
•Mail
58
Best place to work
MEMORY
• Read only data that is needed
DATA STORAGE
• Complex SQL
• Usage of Drop & Keep
• Avoid SASFILE Statement
• Compress Options
SAS Options: Bufsize,Bufno,Fullstimer
• Length Statement
• Creation of Character variables/Formats
59
Best place to work
%Macro Mcall;
%Local cnt; VARIABLE : EXIST/DELETE/SCOPE
%Let Cnt=1;
%MCall2; %LET VAR = 10;
%Put &Cnt &Value; %If %SYMEXIST(VAR) %Then %Do;
%Mend Mcall; …..
%Macro Main; %End
%Let MainM = 10;
%Mcall; %SYMDEL VAR;
%Put &Value &Cnt; %If %SYMLOCAL(VAR) %Then %Do;
%Mend Main; …….
%Main; %End
60
Best place to work
%SYSFUNC,%QSYSFUNC: OPTIONS
Execute SAS functions or user-written functions MACRO
%LET X=%SYSFUNC(TRIM(%SYSFUNC(LEFT(&NUM)))); Controls whether the SAS macro language is available
%SYSGET MERROR
Returns the value of the specified operating environment variable Controls whether the macro processor issues a warning
%Let USERID=%sysget(USER); message when a macro reference cannot be resolved
%put User is &person;
MAUTOSOURCE
RESOLVE :
Controls whether the autocall feature is available
Resolves the value of a text expression during DATA step execution
%let event=Holiday; MCOMPILENOTE
%macro date; New Year %mend date;
Issues a NOTE to the log upon the completion of the
data test; length var1-var3 $ 15;
when='%date'; compilation of a macro
var1=resolve('&event'); /* macro variable reference */
MLOGICNEST
var2=resolve('%date'); /* macro invocation */
var3=resolve(when); /* DATA step variable with macro invocation */ Allows the macro nesting information to be displayed in the
put var1= var2= var3=; run; SAS log
MSTORED
Controls whether stored compiled macros are available
61
Best place to work
EXPRESSIONS LOOPING
%EVAL:- converting its argument from a character value to a %Do %While(Expression);…%End;
numeric or logical expression. Then, it performs the evaluation, Executes a section of a macro repetitively based on the value of an
converts the result back to a character value and returns that value. index variable
Eg. %Do %Until(Expression);… %End;
%Let Value = %Eval(&A+&B); Executes a section of a macro repetitively until a condition is true
%Let ArVal=%Eval(&A>&B); %Do MacroVariable=Start %To Stop <%BY>;… %End;
Executes a section of a macro repetitively based on the value of an
index variable
%SYSEVAL:- Evaluates arithmetic and logical expressions using %Macro RenameVar(Type=);
floating-point arithmetic %Let Cnt=1;
Eg. %Do %Until( &Var1 eq %Str( ));
%Let Var1=%Scan(&AnalyVar,&Cnt,%str( ));
%Let Value = %Eval(10.5+25.6); %if &Var1 Eq %Then %Goto Ext;
%Let ArVal=%Eval(10.5+25.6,CEIL); &Var1=&Var1._&Type
%Let Cnt = %Eval(&Cnt+1);
%IF Expression %THEN …. <%ELSE> … %Ext:
%Macro RptGen(Input=); %End;
%If &Input=Weekly %Then %Do; %Mend RenameVar;
Proc Summary Data = WeekData; Class Prod; Var Balance; Run;
%End; %Let AnalyVar=Test1 Test2 Test3 Test4 Test5;
%Else %If &Input=Monthly %Then %Do; Data Test;
Data MonthEnd; Merge xx1 (Keep=Pm &Analyvar Rename=(%RenameVar(Type=H1)))
Merge MonthEnd(In=Table1) Regions; by CustId; If Table1; xx2 (Keep= Pm&analyvar Rename=(%RenameVar(Type=H2)));
If Region='CA' Then Delete; Run; by Pm;
Proc Summary Data= MonthEnd; Run;
Class Prod; Var Balance; %Macro Clean;
Run; %Do I = 1 %to 25;
%End; %Symdel Prod&I;
%Mend RptGen; %End;
%RptGen(Input=Weekly); %Mend Clean;
62
Best place to work
QUOTING
%STR and %NRSTR : SUPERQ:
Mask the item during macro compilation Retrieves the value of a macro variable from the macro symbol table
and quotes it immediately, preventing the macro processor from
If an unmatched single or double quotation mark or a left or
making any attempt to resolve anything that might occur in the
right parenthesis is used with %STR or %NRSTR, these
resolved value.
characters must be preceded by a percent sign (%).
Data _NULL_;
%let myvar=%str(a%'); X = 'Report Generation For Region1 &Region2';
%Let CurrDate=’31Dec2009’d; Call symput('RTitle',x);
%Let Titletext=%NrStr(Report for Region1 &Region2); Run;
%Let SQ=%Superq(Rtitle);
%Put &Sq;
%BQUOTE and %NRBQUOTE :
Mask values during execution of a macro or a macro language UNQUOTE
statement in open code.
During macro execution, unmasks all special characters and
Mask all the characters, with the addition of unmarked percent mnemonic operators for a value
signs; unmatched, unmarked single and double quotation
marks; and unmatched, unmarked opening and closing %let val = aaa;
parentheses. %let testval = %str(%'&val%');
%Macro test;
data _null_;
%Do I = 1 %to 10; val = %unquote(&testval);
%Let d = %sysfunc(intnx(Month,17834,&i,E),date9.); put 'VAL =' val;
%Let D1 = %bquote('&D'd); %Put &d1;
%End;
run;
%Mend Test;
%test;
SYMBOL TABLE CONTENTS
%if %bquote(&Path) eq %then %do; _ALL_: Describes all currently defined macro variables
%Put ERROR: Path is Missing;
_AUTOMATIC_: Describes all automatic macro variables.
%end;
_GLOBAL_: Describes all user-defined global macro variables
_LOCAL_:Describes user-defined local macro variables
_USER_ : Describes all user-defined macro variables, regardless of scope.
63
Best place to work
65
Best place to work
66
Best place to work
67
Best place to work
SYSERR SYSINFO
0 Execution completed successfully and without warning messages. Proc compare base=Base Compare=New;
4 Execution completed successfully but with warning messages.
Run;
>4 An error occurred. The value returned is procedure dependent.
%Let Compare = %Sysfunc(putn(&SysInfo,binary16.))
%Macro CheckError;
Bit Condition Description
%If &SysErr >=4 %Then EndSas;
%Mend CheckError; 1 DSLABEL Data set labels differ
%Macro DateAdvanced(Unit=,Date=,Advancedby=); 7 BASEOBS Base data set has observation not in comparison
%Let Date=%Sysfunc(Inputn(Date,Date9.));
8 COMPOBS Comparison data set has observation not in base
%If &Unit = DAYS %Then %Do;
%Let Date=%Eval(&Date+&Advancedby); 9 BASEBY Base data set has BY group not in comparison
%Else %Do;
11 BASEVAR Base data set has variable not in comparison
%Let Date=%Sysfunc(Intnx(&Unit,&Date,Advancedby,E));
12 COMPVAR Comparison data set has variable not in base
%End;
%Mend DateAdvance; 13 VALUE A value comparison was unequal
68
Best place to work
70