STS – Customer Insight in Marketing January 2003

Download Report

Transcript STS – Customer Insight in Marketing January 2003

MONSUG
Montreal SAS User Group
SAS v9, Enhancements
Prepared by: Luigi Muro – Consultant
Bell Canada
www.monsug.ca
MONSUG
Formats/Informats
Enhancements to the FORMAT Procedure

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
proc format;
value $genderformat "1"="Female"
"2"="Male";
NOTE: Format $GENDERFORMAT has been output.
Montreal SAS User Group
www.monsug.ca
2
MONSUG
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
Montreal SAS User Group
www.monsug.ca
3
MONSUG
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));
Montreal SAS User Group
www.monsug.ca
4
MONSUG
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);
Montreal SAS User Group
www.monsug.ca
5
MONSUG
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
Montreal SAS User Group
www.monsug.ca
6
MONSUG
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 <=
(code >= 19 and code <= 25) ...;
11) or code = 15 or
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’;
Montreal SAS User Group
www.monsug.ca
7
MONSUG
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.
Montreal SAS User Group
www.monsug.ca
8
MONSUG
Character Functions
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;
Montreal SAS User Group
www.monsug.ca
9
MONSUG
PRX
Perl regular expression (PRX) - Primary functions:
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;
...
;
Montreal SAS User Group
www.monsug.ca
10
MONSUG
PRX
Perl regular expression (PRX) - Examples
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"
Montreal SAS User Group
www.monsug.ca
11
MONSUG
PRX
Perl regular expression (PRX) - Examples
Function
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"
To use the PRX pattern specified with PRXPARSE use PRXMATCH function .
POSITION = PRXMATCH(PATTERN_NUM,STRING);
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
Montreal SAS User Group
www.monsug.ca
12
MONSUG
Montreal SAS User Group
Questions?
www.monsug.ca