<?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 #5 How to add 0’s before a character variable in SAS Community Nordic</title>
    <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-5-How-to-add-0-s-before-a-character-variable/m-p/703848#M325</link>
    <description>&lt;P&gt;The REPEAT function can be really helpful, especially when you want to add 0’s before a character variable. The REPEAT function, &lt;SPAN&gt;&lt;STRONG&gt;REPEAT&lt;/STRONG&gt;&lt;/SPAN&gt;(&lt;A href="https://go.documentation.sas.com/p0p9jw1m2698fvn1ljrnc8b6gij6.htm#p1djlfllec7c3an146xq3nckig0y" target="_blank"&gt;&lt;EM&gt;argument&lt;/EM&gt;&lt;/A&gt;,&amp;nbsp;&lt;A href="https://go.documentation.sas.com/p0p9jw1m2698fvn1ljrnc8b6gij6.htm#n16krkkpmdgoiqn1mo0tsnl8dfsd" target="_blank"&gt;&lt;EM&gt;n&lt;/EM&gt;&lt;/A&gt;), returns a character value consisting of the first argument REPEATed&amp;nbsp;&lt;EM&gt;n&lt;/EM&gt;&amp;nbsp;times.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So first, a simple example on how to use the REPEAT function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
	x=REPEAT('Merry Christmas ', 1);
	put x=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The REPEAT function returns a character value that consist of the first argument repeated&amp;nbsp;&lt;EM&gt;n&lt;/EM&gt;+1 time, hence this output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="pic1.png" style="width: 302px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52352i6E3826DA1D931059/image-size/large?v=v2&amp;amp;px=999" role="button" title="pic1.png" alt="pic1.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The second demonstration show an example on how to add 0’s before a numeric variable ‘y’:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data numeric;
	input x y;
	cards;
7896 44
5431 243
8934 1
6541 675
;
run;

data temp;
	set numeric;
	yy = put(y, z5.);
run; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The z5.&amp;nbsp;is adding ‘k’ zeros before the variable y, so the total length of the new variable ‘yy’ is 5. The ‘k’ is calculated as 5 - number of values in the variable ‘y’, and gives us the following results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="pic2.png" style="width: 222px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52353iBBB1724DD68CA82E/image-size/large?v=v2&amp;amp;px=999" role="button" title="pic2.png" alt="pic2.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The Zw.d format doesn’t work on a character variable, so the last example show how to add 0’s before a character variable ‘y’:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data character;
	input x y$;
	cards;
7896 A44
5431 A243
8934 A1
6541 A675
;
run;

data temp;
	set character;
	yy = cats(REPEAT('0',5-length(y)-1), y);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The CATS function concatenates 0’s with the variable 'y', while the REPEAT function calculates the repeat of 0’s. The repeat of 0’s is calculated as: 5 - number of letters and values in the variable y - 1&lt;STRONG&gt;. &lt;/STRONG&gt;This SAS code gives the following output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="pic3.png" style="width: 266px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52354i23E2D8D6B81A5516/image-size/large?v=v2&amp;amp;px=999" role="button" title="pic3.png" alt="pic3.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 05 Dec 2020 10:48:53 GMT</pubDate>
    <dc:creator>PiaRønnevik</dc:creator>
    <dc:date>2020-12-05T10:48:53Z</dc:date>
    <item>
      <title>Juletip #5 How to add 0’s before a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-5-How-to-add-0-s-before-a-character-variable/m-p/703848#M325</link>
      <description>&lt;P&gt;The REPEAT function can be really helpful, especially when you want to add 0’s before a character variable. The REPEAT function, &lt;SPAN&gt;&lt;STRONG&gt;REPEAT&lt;/STRONG&gt;&lt;/SPAN&gt;(&lt;A href="https://go.documentation.sas.com/p0p9jw1m2698fvn1ljrnc8b6gij6.htm#p1djlfllec7c3an146xq3nckig0y" target="_blank"&gt;&lt;EM&gt;argument&lt;/EM&gt;&lt;/A&gt;,&amp;nbsp;&lt;A href="https://go.documentation.sas.com/p0p9jw1m2698fvn1ljrnc8b6gij6.htm#n16krkkpmdgoiqn1mo0tsnl8dfsd" target="_blank"&gt;&lt;EM&gt;n&lt;/EM&gt;&lt;/A&gt;), returns a character value consisting of the first argument REPEATed&amp;nbsp;&lt;EM&gt;n&lt;/EM&gt;&amp;nbsp;times.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So first, a simple example on how to use the REPEAT function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
	x=REPEAT('Merry Christmas ', 1);
	put x=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The REPEAT function returns a character value that consist of the first argument repeated&amp;nbsp;&lt;EM&gt;n&lt;/EM&gt;+1 time, hence this output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="pic1.png" style="width: 302px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52352i6E3826DA1D931059/image-size/large?v=v2&amp;amp;px=999" role="button" title="pic1.png" alt="pic1.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The second demonstration show an example on how to add 0’s before a numeric variable ‘y’:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data numeric;
	input x y;
	cards;
7896 44
5431 243
8934 1
6541 675
;
run;

data temp;
	set numeric;
	yy = put(y, z5.);
run; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The z5.&amp;nbsp;is adding ‘k’ zeros before the variable y, so the total length of the new variable ‘yy’ is 5. The ‘k’ is calculated as 5 - number of values in the variable ‘y’, and gives us the following results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="pic2.png" style="width: 222px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52353iBBB1724DD68CA82E/image-size/large?v=v2&amp;amp;px=999" role="button" title="pic2.png" alt="pic2.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The Zw.d format doesn’t work on a character variable, so the last example show how to add 0’s before a character variable ‘y’:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data character;
	input x y$;
	cards;
7896 A44
5431 A243
8934 A1
6541 A675
;
run;

data temp;
	set character;
	yy = cats(REPEAT('0',5-length(y)-1), y);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The CATS function concatenates 0’s with the variable 'y', while the REPEAT function calculates the repeat of 0’s. The repeat of 0’s is calculated as: 5 - number of letters and values in the variable y - 1&lt;STRONG&gt;. &lt;/STRONG&gt;This SAS code gives the following output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="pic3.png" style="width: 266px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52354i23E2D8D6B81A5516/image-size/large?v=v2&amp;amp;px=999" role="button" title="pic3.png" alt="pic3.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Dec 2020 10:48:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-5-How-to-add-0-s-before-a-character-variable/m-p/703848#M325</guid>
      <dc:creator>PiaRønnevik</dc:creator>
      <dc:date>2020-12-05T10:48:53Z</dc:date>
    </item>
  </channel>
</rss>

