150 likes | 337 Views
SAS Tips & Techniques. Prepared by: Luigi Muro – Consultant Bell Canada. Tips & Techniques. Tip # 1: Optimization of Data Step GOAL: Reduce the number of data steps required to perform a task. In Create a 1 Flat file containing samples from 2 SAS files. put Files Statistics:
E N D
SAS Tips & Techniques Prepared by: Luigi Muro – Consultant Bell Canada
Tips & Techniques Tip # 1: Optimization of Data Step GOAL: Reduce the number of data steps required to perform a task. In Create a 1 Flat file containing samples from 2 SAS files. put Files Statistics: INPUT1 Number of Records: 2.7 Millions Number of Variables: 52 Variable Length: 257 INPUT2 Number of Records: 4.2 Millions Number of Variables: 52 Variable Length: 257
Tips & Techniques Solution 2 (More Efficient, reduce the number of steps) Data _Null_ ; Set input1.indices(Keep=VAR: where=(ranbin(0, 1, 0.01))) input2.indices(Keep=VAR: where=(ranbin(0, 1, 0.25))) ; File output delimiter=”;”; format var: 8.6; Put … ; Run ; Note: No output file is created. (Not needed since the required output is a flat file) Version 6 (Delimiter option not available) : Put (var:) (8.6,';') ;
Tips & Techniques Tip # 2: Use a SAS procedure to replace many Data Steps GOAL: Produce a sales report: Percent sales by product.
Tips & Techniques Tip # 3: Send an E-mail from SAS (SAS V8) filenamedoemailemail to=('joe.doe@bell.ca' 'jane.doe@bell.ca') cc='gino.tremblay@sympatico.ca' subject='New Transaction File. ' attach='Filename.xls'; data _null_; file doemail; put 'Please note that a new transaction file is available'; run; Note: the following statements must be added to Sasv8.cfg or at invocation -emailsys SMTP or MAPI -emailhost “your.smtp_server.host” (mailserver.bell.ca)
Tips & Techniques Tip # 4 Passing parameters to SAS with SYSPARM option. MVS – (Batch) //RUNSAS EXEC SAS,OPTIONS=“SYSPARM=QUE” //SYSIN DD * %let province = &sysparm; Unix/Windows Batch session SAS -sysin ‘pgm.sas’ -log ‘pgm.log’ -sysparm ‘QUE’ Data _null_; Province = “&sysparm”; Run;
Tips & Techniques Tip # 5. Dates and Strings Manipulations GOAL: List of working days for next week (Exclude Sat. and Sun.). Data _null_; LENGTH datestr $50; DateStr=‘['; DO Dt = (TODAY()) TO (TODAY() + 6) by 1; IF (1 LT WEEKDAY(Dt) LT 7) THEN DateStr=TRIM(DateStr) || PUT(Dt,Date7.) !! ','; END; SUBSTR(DateStr,LENGTH(TRIM(DateStr)),1)=‘]'; CALL SYMPUT(‘Datestr',TRIM(DateStr)); STOP; Run; &Datestr contains [12NOV03,13NOV03,14NOV03,17NOV03,18NOV03]
orders prices customers PartNumber Quantity CustomerID PartNumber UnitPrice Company Address City State CustomerID Tips & Techniques Tip # 6 Use SQL procedure to simplify Merge. GOAL: Merge 3 SAS data sets that do not have a common BY variable.
Tips & Techniques Solution # 2 (Proc SQL) proc sql; select a.CustomerID, Company, Address, b.PartNumber, Quantity, Quantity*UnitPrice as Sale from Customers as a, Prices as b, Orders as c where a.CustomerID = c.CustomerID and b.PartNumber = c.PartNumber order by Company ; quit;
Tips & Techniques Tip # 7a – Use SAS Views to Optimize process (Input views)
Tips & Techniques Tip # 7b – Use SAS Views to Optimize process (Output views) Data_null_ / view=file3; length field1 $ 10 field2 $10 acct $20 balance 8 . . .; set file3; File OUT; Put var1 var2… ; run; proc sort data=file1 out=file3; by balance; run;