1. SPT analytics
“THE KNOWLEDGE TO SUCCEED”
SAS Programming Tip Sheet
SAS Analysis, Consultancy & Outsourcing; Tel: 07402 269 717 Email: scistp@gmail.com
2. N U M E R IC A L F U N C T IO N S C H A R A C T E R F U N C T IO N S SPT analytics
ANALYSIS FIRST >> BETTER RESULTS
A B S(A R G) - R eturns the [po s itiv e] abs o lute v alue A B S(9.12) = 9.12 ; A B S(-6.2) = 6.2 C OM P R ESS(A R G1,… R GN )
,A - R em o v e c hars . F ro m a c har. s tring C OM P R ESS('A B C D ') = 'A B C D '
D IM n(A R G) - N o . o f elem ents in the nth dim enio n o f an array A R R A Y SP T (5) S1-S5; D IM (SP T )=5 IN D EX(A R G1,A R G2) - Searc h s tring A R G1 fo r A R G2 IN D EX('A B C D ','C ')=3
M A X(A R G1, … A R Gn)
, - Value o f s m alles t, no n-m is s ing argum ent M A X(-1.1,7,11) = 11 LEF T (A R G1) - Left align s tring LEF T (' SP T ')='SP T '
M IN (A R G1, … A R Gn)
, - Value o f larges t, no n-m is s ing argum ent M IN (-1,7,11) = -1 LEN GT H (A R G1) - C alc ulates s tring length rem o v ing trailing blank s LEN GT H ('SP T ')=3
M OD (A R G1,A R G2) - R em ainder o f A R G1 div ided by A R G2 M OD (555,10) = 5 LOWC A SE(A R G1) - C o nv ert s tring to lo werc as e LOWC A SE('SP T ')='s pt'
SQR T (A R G1) - Square ro o t o f A R G1 (A R G1 m us t be po s itiv e) SQR T (9) = 3 R EP EA T (A R G1,n) - R epeat s tring A R G1 N +1 tim es R EP EA T ('A ',3)='A A A '
C EIL(A R G) - Sm alles t integer >=A R G1 C EIL(8.7) = 9 C EIL(-5.7) = -5 R EVER SE(A R G1) - R ev ers e o rder o f c hars . in s tring R EVER SE('SP T ')='T P S'
F LOOR (A R G) - Larges t integer <= A R G1 F LOOR (8.7)=8 F LOOR (-5.7)=-6 R IGH T (A R G1) - R ight align s tring R IGH T ('SP T ')=' SP T '
F UZ Z (A R G) - Integer if A R G1 is within 1E-12 o f integer F UZ Z (5.0000000000001)=5 SC A N (A R G1,n) - R eturn nth wo rd fro m A R G1 us ing s td. delim iters SC A N ('S P T ',2)='P '
IN T (A R G) - Integer v alue o f A R G1 IN T (5.7)=5 SUB ST R (A R G1,n,m ) - T ak e m c hars . F ro m A R G1 s tarting fro m c har. N SUB ST R ('SP T ',2,2)='P T '
R OUN D (A R G1, A R G2) - R o unds A R G1 to neares t A R G2 v alue R OUN D (573, 10)=570 T R IM (A R G1) - R em o v e trailing balank s T R IM ('SP T ')='SP T '
UP C A SE(A R G1) - C o nv ert s tring to upperc as e UP C A SE('s pt')='SP T '
S T A T IS T IC A L F U N C T IO N S D A T E & T IM E F U N C T IO N S
C SS(A R G1,… R Gn)
,A - C o rrec ted s um o f s quares C SS(2,4,6,8)=20 D A T E() - R eturns to day's date D A T E()
C V(A R G1,… R Gn)
,A - C o effiec ient o f v ariatio n C V(2,4,6,8)=51.639777949 D A T EP A R T (A R G) - R eturns date fro m a datetim e D A T EP A R T (01J UN 10:13:00:30)=18414
KUR T OSIS(A R G1,… R Gn)
,A - R eturns k urto s is KUR T OSIS(2,4,6,8)=-1.2 D A T ET IM E() - R eturns c urrent datetim e v alue D A T ET IM E()
M EA N (A R G 1,… R Gn)
,A - A v erage o f no n-m is s ing A R G M EA N (2,4,6,8)=5 D A Y(A R G) - R eturns day fro m a SA S date D A Y(18414) = 1
N (A R G 1,… R Gn)
,A - N um ber o f no n-m is s ing v alues N (2,4,6,8)=4 H OUR (A R G) - R eturns ho ur fro m a SA S tim e H OUR (01J UN 10:13:00:30) = 13
N M ISS(A R G 1,… R Gn)
,A - M ax m inus M in N M ISS(2,4,.,8)=1 M D Y(A R G) - D ate v alue fro m m o nth, day, year M D Y(06,01,2010) = 18414
R A N GE(A R G 1,… R Gn)
,A - C alc ulates s k ewnes s R A N GE(2,4,6,8)=6 M IN UT E(A R G) - R eturns m inute fro m a SA S tim e M IN UT E(46830)=0 [13:00:30]
SKEWN ESS(A R G1,… R Gn)
,A Standard dev iatio n o f argum ents SKEWN ESS(2,4,6,8)=0 M ON T H (A R G) - R eturns m o nth fro m a SA S date M ON T H (18414) = 6
ST D (A R G1,… R Gn)
,A - Standard erro r o f m ean ST D (2,4,6,8)=2.5819888975 QT R (A R G) - R eturns quarter fro m a SA S date QT R (18414) = 2
ST D ER R (A R G 1,… R Gn)
,A - Sum o f no n-m is s ing argum ents ST D ER R (2,4,6,8)=1.2909944487 SEC ON D (A R G) - R eturns tim e fro m a SA S datetim e SEC ON D (01J UN 10:13:00:30) = 30
SUM (A R G1,… R Gn)
,A - Sum o f no n-m is s ing A R G SUM (2,4,6,8)=20 T IM E() - R eturns c urrent tim e T IM E()
USS(A R G1,… R Gn)
,A - Unc o rrec t s um o f s quares USS(2,4,6,8)=120 T IM EP A R T (A R G) - R eturns s ec o nd fro m a SA S tim e T IM EP A R T (01J UN 10:13:00:30)=46830
VA R (A R G1,… R Gn)
,A - Varianc e c alc ulatio n VA R (2,4,6,8)=6.6666666667 T OD A Y() - R eturns to day's date T OD A Y()
WEEKD A Y(A R G) - R eturns num ber o f the day o f the week WEEKD A Y(18414) = 3 [T ues day]
M IS C E L L A N O U S YEA R (A R G) - R eturns year fro m a SA S date YEA R (18414) = 2010
D IF n(A R G1) - Ev aluates as A R G1 - LA G(A R G1) IN T C K('IN T ER VA L',F R OM ,T O) N um ber o f interv als in perio d F R OM till T O*
IN P UT (A R G1,IN F OR M A T ) - C o nv ert A R G1 us ing IN F OR M A T IN T N X('IN T ER VA L',F R OM ,n) D ate o r T im e n interv als fro m F R OM *
LA Gn(A R G1) - Ev aluates as A R G1 n ro ws ago *C o m m o nly us ed interv als inc lude; SEC ON D , M N UT E, H OUR ,D A Y, WEEK, M ON T H , QT R , YEA R
M ISSIN G(A R G1) - C hec k s fo r m is s ing A R G1 c an us e fo r num eric and c har
P UT (A R G1,F OR M A T ) - F o rm at A R G1 as F OR M A T N OTES
R A N UN I(s eed) - Generates a rando m num ber bas ed o n s eed in range 0 to 1 SA S dates are the num ber o f days s inc e 1s t J anuary 1960
s ee=0 us es s ys tem c lo c k , s eed>0 allo ws replic atio n SA S tim es are the num ber o f s ec o nds s inc e m idnight
SYM GET (A R G1) - Gets m ac ro v ariable A R G1 SA S datetim es are the num ber o f s ec o nds s inc e 1s t J anuary 1960
SYM P UT (A R G1,A R G2) - Generate m ac ro v ariable A R G1 fro m A R G2 T o c o nv ert a SA S date to an Exc el date add 21916
SAS Analysis, Consultancy & Outsourcing; Tel: 07402 269 717 Email: scistp@gmail.com
3. O P T IO N S U S E F U L C O D IN G E X A M P L E S
SPT analytics
ANALYSIS FIRST >> BETTER RESULTS
F ULLST IM ER / N OF ULLST IM ER R es o urc e us age s tatis tic s inc . C P U and m em o ry /*OUT P UT T O EXC EL VIA OD S*/
SOUR C E / N OSOUR C E P rint SA S s tatem ents to SA S Lo g %let P ath = C :R epo rts ;
SOUR C E2 / N OSOUR C E2 P rint SA S s tatem ents within %IN C LUD E /*B eware o f Exc els default behav io ur o f fo rm atting v alues */
ST IM ER / N OST IM ER R es o urc e s tatis tic s by s tep OD S H T M L F ILE=" &P athSum m ary.xls " ;
T IT LE1 " M o nthly R epo rting Sum m ary" ;
M A C R OGEN / N OM A R C R OGEN P rint m ac ro generated s tatem ents P R O C P R IN T D A T A =F latStats 1 no o bs label; R U N ;
M LOGIC / N OM LOGIC P rint m ac ro exec utio n trac e OD S H T M L C LOSE;
M P R IN T / N OM P R IN T P rint m ac ro s tatem ents o n exec utio n
SYM B OLGEN / N OSYM B OLGEN R es o lv e m ac ro s to SA S Lo g /*R etains exac t fo rm ats o f v alues and dates as defined in the datas et*/
OD S C SVA LL F ILE=" &P athSum m ary.c s v " ;
OB S =10 C o ntro l num ber o f o bs erv atio ns pro c es s ed T IT LE1 " M o nthly R epo rting Sum m ary" ;
OB S=M A X C o ntro l num ber o f o bs erv atio ns pro c es s ed P R O C P R IN T D A T A =F latStats 1 no o bs label; R U N ;
lo c ale=EN _GB Set lo c al area - us e with UK c urrenc y fo rm at [£] OD S C SVA LL C LOSE;
US E F UL F O R M A T S /*OUT P UT T O EXC EL VIA P R OC EXP OR T */
P R O C e xp o r t D A T A =data.F lats tats 1
nlm ny15.2 Set v alue as B ritis h P o und £ £ 10.00 D B M S=exc el2000 R EP LA C E OUT F ILE = " H :T o p T ips flat.xls " ;
nlm nyi15.2 Set v alue as B ritis h P o und GB P GB P 10.00 SH EET = " Lis t F o rm at" ;
D A T E7. D ate fo rm at 01J UN 10 R UN ;
D A T E9. D ate fo rm at 01J UN 2010
M ON YY5. D ate fo rm at J UN 10 /*IN P UT F R OM EXC EL VIA P R OC IM P OR T */
D D M M YY10. D ate fo rm at 40330 P R O C im p o r t o ut=tes t1
D D M M YY8. D ate fo rm at 40330 D B M S=exc el2000 D A T A F ILE = " H :T o p T ips flat.xls " ;
YYM M D D D ate fo rm at 10-06-01 SH EET = " Sum m ary" ;
YYM M D D D ate fo rm at 2010-06-01 R UN ;
YYM M n6. D ate fo rm at 200106
M M YYn4 D ate fo rm at 0610 /*IN P UT D IR EC T F R OM EXC EL - WOR KSH EET C LA SS */
LIB N A M E ex1 'H :D EM O.XLS';
D A T A tem p3;
(k eep = d1-d5) Keep v ariables in num eric al o rder range SET ex1.c las s ;
(k eep = d1--x7) Keep v ariables in po s itio nal o rder range R UN ;
(dro p = lb:) D ro p v ariables beginning with 'lb' LIB N A M E ex1 ;
(dro p = _num eric _) D ro p num eric v ariables
(k eep = _c harac ter_) Keep c harac ter v ariables /*OUT P UT D IR EC T T O EXC EL - WOR KSH EET C LA SS_N EW */
LIB N A M E ex1 'H :D EM O.XLS';
N OTES D A T A ex1.c las s _new;
Exam ples giv en in the dro p and k eep c an by applied in a data s tep and P R OC SQL SET tem p3;
R UN ;
LIB N A M E ex1 ;
SAS Analysis, Consultancy & Outsourcing; Tel: 07402 269 717 Email: scistp@gmail.com
4. U S E F U L C O D IN G E X A M P L E S ( c o n t .)
SPT analytics
ANALYSIS FIRST >> BETTER RESULTS
/*ST R A T IF IED SA M P LE SELEC T ION */ /*C A LC ULA T IN G R A T IOS USIN G D A T A ST EP */ /*SYN T A X C H EC KIN G*/
/*Selec t and s am ple ev ery n ac c o unts */ P R O C S U M M A R Y D A T A =c o s tdetail n s um ;
%let n=10; VA R c o s t ; D A T A big;
D A T A s am ple; OUT P UT OUT =c o s ts um SUM =to talc o s t; SET s m all;
D O pic k it=1 T O to talo bs B Y &n; R UN ; WH ER E gender='M ';
SET s pt.F latfo rm 1 po int=pic k it no bs =to talo bs ; D A T A c alc ulatio ns (k eep=c o s t ratio ); R U N C A N C EL;
OUT P UT ; IF _n_ =1 T H EN SET c o s ts um (k eep=to talc o s t) ;
EN D ; SET c o s tdetail; P R O C S Q L no exec ;
ST OP ; ratio = (c o s t / to talc o s t )*10 0 ; C R EA T E T A B LE c o m bined A S
R UN ; R UN ; SELEC T *
F R OM s ales ;
/*R A N D OM SA M P LE SELEC T ION */ /*C A LC ULA T IN G R A T IOS USIN G SQL*/ Q U IT ;
/*Selec t rando m 100 s am ple*/ P R OC SQL ;
D A T A s am ple2; C R EA T E T A B LE c alc ulatio ns 2 A S
SET s pt.F latfo rm 1; SELEC T *, ((c o s t / s um (c o s t))*10 0 ) as ratio /*F ILE M A IN T EN A N C E EXA M P LES*/
rnd1=ranuni(5 ); F R OM c o s tdetail; /*P R OC D A T A SET S m o re effic iently m ak es am endm ents */
R UN ; Q U IT ; /* to file headers / m etadata. */
P R O C S O R T D A T A =s am ple2; B Y rnd1; R U N ; R UN ; P R O C D A T A S E T S lib=wo rk no lis t ;
D A T A s am ple2; M OD IF Y c alc ulatio ns ;
SET s am ple2; /*M ISC M A C R O VA R IA B LE C A LC ULA T ION S*/ LA B EL c o s t = 'Ov erall C o s t'
IF _n_ <=10 0 ; %let a = 1; ratio = 'C o s t R atio ' ;
R UN ; %let b = 2;
/*Selec t rando m 10% o f datas et*/ %let c = 3.1; M OD IF Y data1;
D A T A s am ple12; %let s tr1 = 'J A N F EB M A R '; R EN A M E c o s t = N ewC o s t;
SET s pt.F latfo rm 1; %let s um 1 = %ev al(&a+&b); = 3 F OR M A T N ewc o s t nlm ny15.2;
IF ranuni(5 ) < 0 .1 T H EN OUT P UT ; %let s um 2 = %s ys ev alf (&a+&b+&c ); = 6.1
R UN ; %let c o unt1 = %s ys func (c o untw(&s tr1)); =3 D ELET E c alc ulatio ns 2;
/*Selec t rando m 250 o bs . s am ple*/ /*P rint all us er defined m ac ro v ariables */ Q U IT ;
P R O C S U R V E Y S E L E C T data=F latfo rm 1 %put _us er_; R UN ;
n=250 o ut=s am ple;
R UN ; /*M ET H OD S T O D ELA Y C OD E EXEC UT ION */ /*C lear Output windo w */
/*Sleep SA S fo r 5 s ec o nds */ dm 'c le OUT P UT ';
/*USE A R R A YS T O C LEA N D EF A ULT VA LUES*/ D A T A _null_ ;
D A T A s am ple1 (dro p=j); x=s leep(5 ); /*C lear Lo g windo w */
SET s am ple; R UN ; dm 'c le lo g';
A R R A Y c lean{*} tc r1-tc r3 lb1 _T EM P OR A R Y_;
D O j=1 T O dim (c lean); /*Sleep SA S until datetim e */ /*D elete M ac ro Variable p */
IF c lean(j)=-9 9 9 9 9 9 9 T H EN c lean(j)=0 ; D A T A _null_; %LET p=10;
EN D ; s lept=wak eup(' 0 1J U N 2 0 10 :10 :3 0 :0 0 ' d t ); %SYM D EL p;
R UN ; R UN ; %P UT &p;
SAS Analysis, Consultancy & Outsourcing; Tel: 07402 269 717 Email: scistp@gmail.com