SCAN Function | SAS Character Functions

 SCAN Function returns the nth word from a character string.

Syntax - 

SCAN(string/Variable, count, Delimiter, Modifier);

String/Variable  - 

This is the source Value from where we want to extract the nth Word.

Count -

Non zero positive or negative numeric value. If count is positive, SCAN function start search from left to right. If count is negative, SCAN function start search from right to left.

Delimiter - 

It's optional argument, any Character value or Special Character can be used as a delimiter, if left blank then the default delimiters are ! $ % & ( ) * + , - . / ; < ^ ~ |

Modifier

Specifies a character constant, variable, or expression in which each non-blank character modifies the action of the SCAN function. Blanks are ignored. 

Example - 

DATA A;
Title_Name='Datahark - Every Data is a Story';
First_word=scan(Title_Name,1);
Second_word=scan(Title_Name,2);
a=scan(Title_Name,2,'-');
Last_word=scan(Title_Name,-1);
Second_Last_word=scan(Title_Name,-2);
b=scan(Title_Name,-2);
run;

Proc Print Data=a; 
run;


 

See the Value of each variable in output and understand the difference - 

  • First_word=scan(Title_Name,1);  -> This Extract the First string i.e. Datahark 
  • Second_word=scan(Title_Name,2); -> This Extract the second string i.e. Every
  • a=scan(Title_Name,2,'-'); -> We have specified delimiter as '-' so it considered ' Every Data is a Story' as a second value.
  • Last_word=scan(Title_Name,-1); -> Negative count Extracted last Value: Story
  • Second_Last_word=scan(Title_Name,-2); -> Negative count Extracted second last Value: a
  • b=scan(Title_Name,-2,'-'); -> Negative count and Delimiter is '-' so it considered ' Every Data is a Story' as first word and we have specified -2 in count so gives second last value as 'Datahark '

Subscribe to DataHark by Email

Post a Comment

0 Comments