CSULB_STAT475_handout16

CSULB_STAT475_handout16 - STAT 475 Chapter 14 MODIFYING...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
STAT 475 Chapter 14 MODIFYING CHARACTER VARIABLE VALUES WITH SAS FUNCTIONS The function SCAN returns a specified word from a character variable value. The syntax is scan( variable_name , word_number , delimiters ) where delimiters are special characters used to separate character expressions. They must be enclosed in single quotation marks. Default delimiters are blank , . < ( ) + | & ! $ * ; ^ - / % Note that if delimiters in the function SCAN belong to the list of the default ones, then they may be omitted. The syntax then is scan( variable_name , word_number ) Example. The following code separates students’ names as written in an official class roster into last name and first name and drops the middle name. Note that the delimiters used to separate the names are comma and blank, which are default delimiters and may be omitted. data roster (drop=name); input student_id$ 1-9 @11 name$ 21.; last_name=scan(name,1); first_name=scan(name,2); cards; 001039133 Nguyen, Melany 679834566 Mari, Leana Adrienne 113816433 Cawley, Anne Margaret ; proc print noobs; run; The output is student_ last_ first_ id name name 001039133 Nguyen Melany 679834566 Mari Leana 113816433 Cawley Anne The function SUBSTR is used to (i) extract a portion of a character variable value; (ii) replace the contents of a character variable value. 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
(i) To extract a portion of a character variable value, use the syntax new_variable =substr( variable_name , position , n ) where position is the character position to start from, and n is the number of characters to extract. If n is omitted, all remaining characters are included in the substring. Example. In the roster example, the following code extracts the last four digits of the student id numbers. data roster1 (drop=student_id); set roster; id_last4=substr(student_id, 6, 4); run; proc print noobs; run; The output is last_ first_ name name id_last4 Nguyen Melany 9133 Mari Leana 4566 Cawley Anne 6433 (ii) To replace the contents of a character variable value, use the syntax substr( variable_name , position , n )= ' new_string ' where position is the character position to start replacement from, n is the number of characters to replace, and the new_string is a substring to replace with. If n is omitted, all remaining characters are replaced. Example.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/14/2011 for the course STAT 475 taught by Professor Olgak during the Fall '11 term at CSU Long Beach.

Page1 / 8

CSULB_STAT475_handout16 - STAT 475 Chapter 14 MODIFYING...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online