<?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 Re: Create a fixed length character field from a variable length field in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-fixed-length-character-field-from-a-variable-length/m-p/558347#M155826</link>
    <description>Thanks Tom! Great solution, plus I can adapt it for longer strings.</description>
    <pubDate>Mon, 13 May 2019 15:55:21 GMT</pubDate>
    <dc:creator>ghosh</dc:creator>
    <dc:date>2019-05-13T15:55:21Z</dc:date>
    <item>
      <title>Create a fixed length character field from a variable length field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-fixed-length-character-field-from-a-variable-length/m-p/558179#M155757</link>
      <description>&lt;P&gt;I would like to convert a char variable (KID) consisting of one or two digits and sometimes a letter appended to it, into a 5 digit character string starting with the letter K followed by the existing variable with zeroes inserted in between (_ID).&amp;nbsp; Ideally I would like to use a user-defined format to do the conversion or perhaps a more efficient approach than what I currently have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	Select;
		When (length(KID)=1) Pad='000';
		When (length(KID)=2) Pad='00';
		otherwise Pad='0';
	End;
		_ID = cats('K',Pad,KID);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here are the Have (KIDS) and Want (_ID) variables:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;KID _ID&lt;BR /&gt;1a K001a&lt;BR /&gt;1b K001b&lt;BR /&gt;2a K002a&lt;BR /&gt;2b K002b&lt;BR /&gt;3 K0003&lt;BR /&gt;4 K0004&lt;BR /&gt;5 K0005&lt;BR /&gt;6 K0006&lt;BR /&gt;7 K0007&lt;BR /&gt;8 K0008&lt;BR /&gt;9 K0009&lt;BR /&gt;10 K0010&lt;BR /&gt;11 K0011&lt;BR /&gt;11a K011a&lt;BR /&gt;11b K011b&lt;BR /&gt;12a K012a&lt;BR /&gt;12b K012b&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2019 03:26:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-fixed-length-character-field-from-a-variable-length/m-p/558179#M155757</guid>
      <dc:creator>ghosh</dc:creator>
      <dc:date>2019-05-13T03:26:49Z</dc:date>
    </item>
    <item>
      <title>Re: Create a fixed length character field from a variable length field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-fixed-length-character-field-from-a-variable-length/m-p/558180#M155758</link>
      <description>&lt;P&gt;You could use the REPEAT function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length _id $5;
_id='K'||repeat('0',3-length(kid))||kid;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 May 2019 03:49:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-fixed-length-character-field-from-a-variable-length/m-p/558180#M155758</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-13T03:49:35Z</dc:date>
    </item>
    <item>
      <title>Re: Create a fixed length character field from a variable length field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-fixed-length-character-field-from-a-variable-length/m-p/558287#M155797</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input KID $ _ID $;
length temp $ 4;
temp=kid;
want=cats('K',translate(right(temp),'0',' '));
cards;
1a K001a
1b K001b
2a K002a
2b K002b
3 K0003
4 K0004
5 K0005
6 K0006
7 K0007
8 K0008
9 K0009
10 K0010
11 K0011
11a K011a
11b K011b
12a K012a
12b K012b
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 May 2019 13:05:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-fixed-length-character-field-from-a-variable-length/m-p/558287#M155797</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-05-13T13:05:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create a fixed length character field from a variable length field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-fixed-length-character-field-from-a-variable-length/m-p/558347#M155826</link>
      <description>Thanks Tom! Great solution, plus I can adapt it for longer strings.</description>
      <pubDate>Mon, 13 May 2019 15:55:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-fixed-length-character-field-from-a-variable-length/m-p/558347#M155826</guid>
      <dc:creator>ghosh</dc:creator>
      <dc:date>2019-05-13T15:55:21Z</dc:date>
    </item>
  </channel>
</rss>

