<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Juletip #6 Using SAS functions on the Christmas song Jingle Bells in SAS Community Nordic</title>
    <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-6-Using-SAS-functions-on-the-Christmas-song-Jingle-Bells/m-p/784245#M361</link>
    <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;So, first we will u&lt;SPAN&gt;se the DATALINES statement with an INPUT statement to read the two first stanza of the song.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options linesize=256;

data jingle_bells;
	infile datalines delimiter=',' dsd truncover;
	input stanza:1. Text:$500.;
	datalines4;
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
;;;;

Data Jingle_BellsN(drop=Text_o);
	set Jingle_Bells(rename=(Text=Text_o));
	Text=upcase(Text_o);
run;

proc print data=Jingle_BellsN;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bilde1.PNG" style="width: 961px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66424iF254024C22871EE4/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bilde1.PNG" alt="Bilde1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The word Bells appears a lot of times in this Christmas song.&amp;nbsp;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.&lt;/P&gt;
&lt;P&gt;Also it’s interesting to find the word after Bells, to see if there is a pattern.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Multiple_BELLS(drop=PrevBELLSWordNum);
	set Jingle_BellsN;

	/*Counts the number of times that the word BELLS appears within the text*/
	NumBELLS=count(Text,'BELLS');

	/* Returns the starting position where the word BELLS is found the text, counting delimeters also*/
	BELLSWordPos=find(Text,'BELLS');

	/*The e modifier counts the words that are scanned until BELLS is found, 
	instead of the starting point, without counting delimeters='- .,'*/
	BELLSWordNum=findw(Text,'BELLS','- .,','e');

	/*find the word after the key word BELLS*/
	if BELLSWordNum&amp;gt;0 then
		AfterBELLS=scan(Text,BELLSWordNum+1,'- .,');
	output;

	/*Multiple Occurences*/
	do while(BELLSWordPos&amp;gt;0);
		PrevBELLSWordNum=BELLSWordNum;
		BELLSWordNum=findw(Text,'BELLS','- ., ',BELLSWordPos+1,'e')+PrevBELLSWordNum;
		BELLSWordPos=find(Text,'BELLS',BELLSWordPos+1);

		if BELLSWordPos&amp;gt;0 then
			do;
				AfterBELLS=scan(Text,BELLSWordNum+1,'- .,');
				output;
			end;
	end;
run;

proc print data=Multiple_BELLS;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bilde2.PNG" style="width: 974px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66425i17C2EEEC677B6223/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bilde2.PNG" alt="Bilde2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;This could be done by the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Dividing_Text;
	set Jingle_BellsN;

	/* counts the number of times the ',-' appears the character string Common_names */
	SpecCharNum=countc(Text,',-');

	if SpecCharNum=0 then
		do;
			Name=Text;
			output;
		end;

	/* writes first part out before the delimeters, then second part, and so on...*/
	else
		do i=1 to SpecCharNum+1;
			Name=scan(Text,i,',-');
			output;
		end;
run;

proc print data=Dividing_Text;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bilde3.PNG" style="width: 968px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66426i60DE339769E171CC/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bilde3.PNG" alt="Bilde3.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Maybe, I am a song writer and want to compose a new version of the Christmas song.&lt;/P&gt;
&lt;P&gt;I can easily substitute words in the song by using PRXCHANGE functions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data BELLS_substitution(drop=Text);
	format Name_New $500.;
	set Jingle_BellsN;

	/*S=Specifies a substitution in regular expression, PRXCHANGE function performs a substitution for a pattern match*/
	Name_New=prxchange('s/BELLS/ STAR/',-1,Text);
	Name_New=prxchange('s/ RIDE/ SLIDE/',-1,Name_New);
run;

proc print data=BELLS_substitution;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bilde4.PNG" style="width: 971px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66427i28F3B993DF955460/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bilde4.PNG" alt="Bilde4.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;So, in my family we are singing the new ‘hit’ song Jingle Star this Year. &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I wish you all a Merry Christmas and Happy New Year!&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":evergreen_tree:"&gt;🌲&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":wrapped_gift:"&gt;🎁&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":evergreen_tree:"&gt;🌲&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 08 Dec 2021 13:36:46 GMT</pubDate>
    <dc:creator>PiaRønnevik</dc:creator>
    <dc:date>2021-12-08T13:36:46Z</dc:date>
    <item>
      <title>Juletip #6 Using SAS functions on the Christmas song Jingle Bells</title>
      <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-6-Using-SAS-functions-on-the-Christmas-song-Jingle-Bells/m-p/784245#M361</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;So, first we will u&lt;SPAN&gt;se the DATALINES statement with an INPUT statement to read the two first stanza of the song.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options linesize=256;

data jingle_bells;
	infile datalines delimiter=',' dsd truncover;
	input stanza:1. Text:$500.;
	datalines4;
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
;;;;

Data Jingle_BellsN(drop=Text_o);
	set Jingle_Bells(rename=(Text=Text_o));
	Text=upcase(Text_o);
run;

proc print data=Jingle_BellsN;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bilde1.PNG" style="width: 961px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66424iF254024C22871EE4/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bilde1.PNG" alt="Bilde1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The word Bells appears a lot of times in this Christmas song.&amp;nbsp;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.&lt;/P&gt;
&lt;P&gt;Also it’s interesting to find the word after Bells, to see if there is a pattern.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Multiple_BELLS(drop=PrevBELLSWordNum);
	set Jingle_BellsN;

	/*Counts the number of times that the word BELLS appears within the text*/
	NumBELLS=count(Text,'BELLS');

	/* Returns the starting position where the word BELLS is found the text, counting delimeters also*/
	BELLSWordPos=find(Text,'BELLS');

	/*The e modifier counts the words that are scanned until BELLS is found, 
	instead of the starting point, without counting delimeters='- .,'*/
	BELLSWordNum=findw(Text,'BELLS','- .,','e');

	/*find the word after the key word BELLS*/
	if BELLSWordNum&amp;gt;0 then
		AfterBELLS=scan(Text,BELLSWordNum+1,'- .,');
	output;

	/*Multiple Occurences*/
	do while(BELLSWordPos&amp;gt;0);
		PrevBELLSWordNum=BELLSWordNum;
		BELLSWordNum=findw(Text,'BELLS','- ., ',BELLSWordPos+1,'e')+PrevBELLSWordNum;
		BELLSWordPos=find(Text,'BELLS',BELLSWordPos+1);

		if BELLSWordPos&amp;gt;0 then
			do;
				AfterBELLS=scan(Text,BELLSWordNum+1,'- .,');
				output;
			end;
	end;
run;

proc print data=Multiple_BELLS;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bilde2.PNG" style="width: 974px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66425i17C2EEEC677B6223/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bilde2.PNG" alt="Bilde2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;This could be done by the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Dividing_Text;
	set Jingle_BellsN;

	/* counts the number of times the ',-' appears the character string Common_names */
	SpecCharNum=countc(Text,',-');

	if SpecCharNum=0 then
		do;
			Name=Text;
			output;
		end;

	/* writes first part out before the delimeters, then second part, and so on...*/
	else
		do i=1 to SpecCharNum+1;
			Name=scan(Text,i,',-');
			output;
		end;
run;

proc print data=Dividing_Text;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bilde3.PNG" style="width: 968px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66426i60DE339769E171CC/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bilde3.PNG" alt="Bilde3.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Maybe, I am a song writer and want to compose a new version of the Christmas song.&lt;/P&gt;
&lt;P&gt;I can easily substitute words in the song by using PRXCHANGE functions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data BELLS_substitution(drop=Text);
	format Name_New $500.;
	set Jingle_BellsN;

	/*S=Specifies a substitution in regular expression, PRXCHANGE function performs a substitution for a pattern match*/
	Name_New=prxchange('s/BELLS/ STAR/',-1,Text);
	Name_New=prxchange('s/ RIDE/ SLIDE/',-1,Name_New);
run;

proc print data=BELLS_substitution;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bilde4.PNG" style="width: 971px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66427i28F3B993DF955460/image-size/large?v=v2&amp;amp;px=999" role="button" title="Bilde4.PNG" alt="Bilde4.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;So, in my family we are singing the new ‘hit’ song Jingle Star this Year. &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I wish you all a Merry Christmas and Happy New Year!&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":evergreen_tree:"&gt;🌲&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":wrapped_gift:"&gt;🎁&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":evergreen_tree:"&gt;🌲&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Dec 2021 13:36:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-6-Using-SAS-functions-on-the-Christmas-song-Jingle-Bells/m-p/784245#M361</guid>
      <dc:creator>PiaRønnevik</dc:creator>
      <dc:date>2021-12-08T13:36:46Z</dc:date>
    </item>
  </channel>
</rss>

