www.monsug.ca montreal sas user group monsug sas v9, enhancements prepared by:luigi muro –...
TRANSCRIPT
www.monsug.ca
Montreal SAS User Group MONSUG
SAS v9, Enhancements
Prepared by: Luigi Muro – Consultant
Bell Canada
2
MONSUG
www.monsug.ca
Montreal SAS User Group
Formats/Informats
Enhancements to the FORMAT Procedure
proc format; value $genderformat "1"="Female" "2"="Male";NOTE: Format $GENDERFORMAT has been output.
FORMAT and INFORMATS with longer names. 32 Characters for Numeric formats. 31 Characters for Character formats (allows for a $
sign).
Note: Not compatible with version 8
3
MONSUG
www.monsug.ca
Montreal SAS User Group
Character Functions
New Character SAS Functions
STRIP Strips leading and trailing blanks from a string
CAT Concatenates character strings without removing leading or trailing blanks
CATS Concatenates character strings and removes leading and trailing blanks
CATT Concatenates character strings and removes trailing blanks
CATX Concatenates strings, removes leading and trailing blanks, and inserts separators
PROPCASE Converts all words in an argument to proper case
CHAR Extracts a single character from a string
FIRST Extracts the first character from a string
4
MONSUG
www.monsug.ca
Montreal SAS User Group
Character Functions
Concatenating Strings
Have you ever used || (concat), TRIM, LEFT, and PUT functions.
Version 8 and below: combine = trim(left(Char1)) || ‘ ‘ || left(Char2);
remblanks = trim(left(put(1350.624, 8.2)));
Version 9 (Reduce code complexity)combine = catx(‘ ‘, Char1, Char2);
remblanks = strip(put(1350.624, 8.2));
5
MONSUG
www.monsug.ca
Montreal SAS User Group
Macro Functions
CREATING MACRO VARIABLES
Commonly include LEFT, TRIM and PUT functions to make sure that the macro variable value is constructed properly.
Version 8 and below:
call symput(‘Max’, trim(left(put(maxValue , 8.))) );
Version 9 CALL SYMPUTX handles left justification, trimming, and character conversion.
call symputx( ‘Max’ , maxValue);
6
MONSUG
www.monsug.ca
Montreal SAS User Group
Character Functions
The PROPCASE Function
The PROPCASE function returns a string in proper (mixed) case.
Example:
line = 'macro-generated line';
line1 = PROPCASE(line, ' ');
line2 = PROPCASE(line, ' -');
Result:
line1 = Macro-generated Line
line2 = Macro-Generated Line
7
MONSUG
www.monsug.ca
Montreal SAS User Group
Data step – IN operator
Integer ranges can be specified with an IN operator
Version 8 and below:
A. if code in (3,7,8,9,10,11,15,19,20,21,22,23,24,25) then put ‘Found’;
B. if code = 3 or (code >= 7 and code <= 11) or code = 15 or (code >= 19 and code <= 25) ...;
C. if code = 3 or (7 <= code <= 11) or code = 15 or (19 <= code <= 25) ...;
Version 9 (Reduce code complexity)
if code in (3, 7:11, 15, 19:25) then put ‘Found’;
8
MONSUG
www.monsug.ca
Montreal SAS User Group
Data step - Putlog
PUTLOG - Writes a message to the SAS log
Data _null_ ; file print ; put 'This goes to OUTPUT window' ; putlog ‘This message goes to the LOG' ; putlog 'WARNING: ...' ; Line displayed as Green in log window putlog 'ERROR: ...‘ ; Line displayed as Red in log window put 'This goes to OUTPUT window' ;run ;
Difference between Put and Putlog:
PUTLOG explicitly writes to the SAS LOG. This means that you can direct regular PUT statements to another destination, and write to the log using PUTLOG without the need to redirect output to the LOG with a FILE LOG statement.
9
MONSUG
www.monsug.ca
Montreal SAS User Group
Extract a single character from a string using CHAR and FIRST Version 8 and Below:
Data extract; MarketingCode='FD12Q1320'; Region=substr(MarketingCode, 5, 1); Product=substr(MarketingCode, 1, 1);run;
Version 9:
Data extract; MarketingCode='FD12Q1320'; Region=CHAR(MarketingCode, 5); Product=FIRST(MarketingCode);Run;
Character Functions
10
MONSUG
www.monsug.ca
Montreal SAS User Group
Perl regular expression (PRX) - Primary functions:
PRX
PRXPARSE: To define a Perl regular expression to be used later by the other regular expression functions.
Combination of alphanumeric characters, strings and metacharacters.
PRXMATCH: Locate the position in a string, where a regular expression match is found. This function returns the first position in a string. If this pattern is not found, the function returns a zero.
Other functions and call routines include: PRXSUBSTR, PRXPOSN, PRXNEXT, PRXPAREN
DATA _NULL_; IF _N_ = 1 THEN PATTERN_NUM = PRXPARSE("/cat/"); RETAIN PATTERN_NUM; INPUT STRING $30.; POSITION = PRXMATCH(PATTERN_NUM,STRING); DATALINES; ...;
11
MONSUG
www.monsug.ca
Montreal SAS User Group
Perl regular expression (PRX) - Examples
PRX
Metacharacter Description Examples
* Matches the previous subexpression zero or more times
cat* matches "cat", "cats", "catanddog"
+ Matches the previous subexpression one or more times
\d+ matches one or more digits
? Matches the previous subexpression zero or one times
hello? matches "hell" and "hello"
. (period) Matches exactly one character r.n matches "ron", "run", and "ran"
\d Matches a digit 0 to 9 \d\d\d matches any three digit number
\D Matches a non-digit \D\D matches "xx", "ab" and "%%"
^ Matches the beginning of the string ^cat matches "cat" and "cats" but not "the cat"
$ Matches the end of a string cat$ matches "the cat" but not "cat in the hat"
12
MONSUG
www.monsug.ca
Montreal SAS User Group
Perl regular expression (PRX) - ExamplesFunction Matches Does not Match
PRXPARSE("/cat/") "The cat is black" "cats"
PRXPARSE("/^cat/") "cat on the roof" "The cat"
PRXPARSE("/cat$/") "There is a cat" "cat in the house"
PRX
Interested in learning more. Excellent SUGI 29 paper @
http://www2.sas.com/proceedings/sugi29/265-29.pdf
Title: An Introduction to Perl Regular Expressions in SAS 9
Author: Ron Cody, Robert Wood Johnson Medical School, Piscataway, NJ
To use the PRX pattern specified with PRXPARSE use PRXMATCH function .POSITION = PRXMATCH(PATTERN_NUM,STRING);
www.monsug.ca
Montreal SAS User Group MONSUG
Questions?