SAS functions are incredibly powerful in a Data step. This Juletip will look at the strength of functions on text strings, and since it’s December it would be fun to look at the famous Christmas song Jingle Bells.
So, first we will use the DATALINES statement with an INPUT statement to read the two first stanza of the song.
infile datalines delimiter=',' dsd truncover;
input stanza:1. Text:$500.;
1,Dashing through the snow - In a one horse open sleigh - O'er the fields we go - Laughing all the way - Bells on bob tails ring - Making spirits bright - What fun it is to laugh and sing - A sleighing song tonight
2,Oh jingle bells - jingle bells - Jingle all the way - Oh what fun it is to ride - In a one horse open sleigh - Jingle bells jingle bells - Jingle all the way - Oh what fun it is to ride - In a one horse open sleigh
proc print data=Jingle_BellsN;
The word Bells appears a lot of times in this Christmas song. It would be great to count the number of times it appears in each of the two stanza, together with the starting positions of the word Bells.
Also it’s interesting to find the word after Bells, to see if there is a pattern.
/*Counts the number of times that the word BELLS appears within the text*/
/* Returns the starting position where the word BELLS is found the text, counting delimeters also*/
/*The e modifier counts the words that are scanned until BELLS is found,
instead of the starting point, without counting delimeters='- .,'*/
/*find the word after the key word BELLS*/
if BELLSWordNum>0 then
BELLSWordNum=findw(Text,'BELLS','- ., ',BELLSWordPos+1,'e')+PrevBELLSWordNum;
if BELLSWordPos>0 then
proc print data=Multiple_BELLS;
But what if we want to split the stanza into different parts as verse, so we get one row for each verse in the Christmas song.
This could be done by the following code:
/* counts the number of times the ',-' appears the character string Common_names */
if SpecCharNum=0 then
/* writes first part out before the delimeters, then second part, and so on...*/
do i=1 to SpecCharNum+1;
proc print data=Dividing_Text;
Maybe, I am a song writer and want to compose a new version of the Christmas song.
I can easily substitute words in the song by using PRXCHANGE functions:
format Name_New $500.;
/*S=Specifies a substitution in regular expression, PRXCHANGE function performs a substitution for a pattern match*/
Name_New=prxchange('s/ RIDE/ SLIDE/',-1,Name_New);
proc print data=BELLS_substitution;
So, in my family we are singing the new ‘hit’ song Jingle Star this Year. 😉
I wish you all a Merry Christmas and Happy New Year! 🌲🎁🌲
Take a look at the autumn/winter 2020 program and events for FANS and other SAS users! www.sas.com/fans | #SASFANS #sasnordicusers
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.