<?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: split sas values in two but keep the leading 0's and space in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613307#M179099</link>
    <description>&lt;P&gt;Does the source variable always have 4 digits, space and then 5 digits?&amp;nbsp; Is then just use SUBSTR() to split the field.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;site=substr(subject_code,1,4);
subj=substr(subject_code,6,5);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or are you trying to create numeric variables instead of character variables?&amp;nbsp; Numeric variables do not have any concept of leading zeros.&amp;nbsp; But you can attach a format that will make the values print with leading zeros.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;site=input(subject_code,4.);
subj=input(substr(subject_code,6),5.);
format site z4. subj z5.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 20 Dec 2019 19:28:35 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-12-20T19:28:35Z</dc:date>
    <item>
      <title>split sas values in two but keep the leading 0's and space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613301#M179095</link>
      <description>&lt;P&gt;My dataset looks like this&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://i.stack.imgur.com/KB0lW.png" border="0" alt="Variable image" /&gt;&lt;/P&gt;&lt;P&gt;And I want it to look like this&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt; Subject Code  site          subj
 0156 00062    156            62 
 0156 00062    156            62 
 0047 00032    47             32
 0034 00066    34             66
 0032 00029    32             29 
 .
 .  &lt;/CODE&gt;&lt;/PRE&gt;&lt;UL&gt;&lt;LI&gt;My Code: if "Subject Code"n ^="" then site=input(scan("Subject Code"n,1,' '),z9.);&lt;BR /&gt;put site=;&lt;BR /&gt;if "Subject Code"n ^="" thensubj=input(strip(substr((scan("Subject Code"n,-1)),1,4)),$4.);&lt;BR /&gt;put subj=;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;The Output i get:&lt;/P&gt;&lt;P&gt;site=15600062&lt;/P&gt;&lt;P&gt;subj=1560&lt;/P&gt;&lt;P&gt;As you can see sas takes out the leading 0 values and the space " ", because of which it's difficult to split.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2019 19:13:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613301#M179095</guid>
      <dc:creator>Kp1234</dc:creator>
      <dc:date>2019-12-20T19:13:04Z</dc:date>
    </item>
    <item>
      <title>Re: split sas values in two but keep the leading 0's and space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613304#M179096</link>
      <description>&lt;P&gt;One way&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input subjectcode $&amp;amp;10.;
datalines;
0156 00062
0156 00062
0047 00032
0034 00066
0032 00029
;

data want;
   set have;
   site=prxchange('s/0*([1-9]+) 0*([1-9]+)/$1/', -1, subjectcode);
   subj=prxchange('s/0*([1-9]+) 0*([1-9]+)/$2/', -1, subjectcode);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Dec 2019 19:21:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613304#M179096</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-12-20T19:21:08Z</dc:date>
    </item>
    <item>
      <title>Re: split sas values in two but keep the leading 0's and space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613306#M179098</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/304341"&gt;@Kp1234&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the compress function to remove the zeros :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines truncover;
	input "Subject Code"n $10.;
	datalines;
0156 00062
0156 00062
0047 00032
0034 00066
0032 00029
;
run;

data want;
	set have;
	if "Subject Code"n ^="" then do;
		site = input(compress(scan("Subject Code"n,1,' '),"0"),best.);
		subj = input(compress(scan("Subject Code"n,-1),"0"),best.);
	end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Dec 2019 19:26:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613306#M179098</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-20T19:26:43Z</dc:date>
    </item>
    <item>
      <title>Re: split sas values in two but keep the leading 0's and space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613307#M179099</link>
      <description>&lt;P&gt;Does the source variable always have 4 digits, space and then 5 digits?&amp;nbsp; Is then just use SUBSTR() to split the field.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;site=substr(subject_code,1,4);
subj=substr(subject_code,6,5);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or are you trying to create numeric variables instead of character variables?&amp;nbsp; Numeric variables do not have any concept of leading zeros.&amp;nbsp; But you can attach a format that will make the values print with leading zeros.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;site=input(subject_code,4.);
subj=input(substr(subject_code,6),5.);
format site z4. subj z5.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2019 19:28:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613307#M179099</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-20T19:28:35Z</dc:date>
    </item>
    <item>
      <title>Re: split sas values in two but keep the leading 0's and space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613308#M179100</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
 data have;
 input Subject_Code $12.;
 cards;
 0156 00062    156            62 
 0156 00062    156            62 
 0047 00032    47             32
 0034 00066    34             66
 0032 00029    32             29 
 ;

data want;
  set have;
  length subj site $5;
  subj=substr(scan(subject_code,1),verify(scan(subject_code,1),'0'));
  site=substr(scan(subject_code,-1),verify(scan(subject_code,-1),'0'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Dec 2019 19:29:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613308#M179100</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-20T19:29:02Z</dc:date>
    </item>
    <item>
      <title>Re: split sas values in two but keep the leading 0's and space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613309#M179101</link>
      <description>&lt;P&gt;Thank you for your reply,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using the proc import to read a text file which contains the variable&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC IMPORT OUT=manifest&lt;BR /&gt;datafile="../../sd/accumulative/manifests/manifestcomb.txt"&lt;BR /&gt;REPLACE ;&lt;BR /&gt;delimiter='09'x;&lt;BR /&gt;guessingrows=32767;&lt;BR /&gt;RUN;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when sas reads the variable "Study Code"n (This is one variable), it takes out space and the leading 0's Eg:&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;0156 00062&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;becomes: 15600062,&lt;/P&gt;&lt;P&gt;is there something that i need to do in the PROC import to keep the formatting?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2019 19:42:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613309#M179101</guid>
      <dc:creator>Kp1234</dc:creator>
      <dc:date>2019-12-20T19:42:35Z</dc:date>
    </item>
    <item>
      <title>Re: split sas values in two but keep the leading 0's and space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613310#M179102</link>
      <description>&lt;P&gt;thank you for all your reply, much appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using the proc import to read a text file which contains the variable&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC IMPORT OUT=manifest&lt;BR /&gt;datafile="../../sd/accumulative/manifests/manifestcomb.txt"&lt;BR /&gt;REPLACE ;&lt;BR /&gt;delimiter='09'x;&lt;BR /&gt;guessingrows=32767;&lt;BR /&gt;RUN;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when sas reads the variable "Study Code"n (This is one variable), it takes out space and the leading 0's Eg:&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;0156 00062&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;becomes: 15600062,&lt;/P&gt;&lt;P&gt;is there something that I need to do in the PROC import to keep the formatting?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2019 19:44:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613310#M179102</guid>
      <dc:creator>Kp1234</dc:creator>
      <dc:date>2019-12-20T19:44:16Z</dc:date>
    </item>
    <item>
      <title>Re: split sas values in two but keep the leading 0's and space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613312#M179103</link>
      <description>&lt;P&gt;You're on the right track.&amp;nbsp; These are nominal numbers, so keep them as alpha&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	length site subj $5;
	infile datalines;
	input @1 "Subject Code"n $11.;

	if "Subject Code"n ^="" then
		site=scan("Subject Code"n, 1);
	put site=;

	if "Subject Code"n ^="" then
		subj=scan("Subject Code"n, -1);
	put subj=;
	datalines;
 0156 00062    
 0156 00062    
 0047 00032    
 0034 00066    
 0032 00029   
 
 
;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Annotation 2019-12-20 144523.png" style="width: 430px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34979iFE947BED656C204A/image-size/large?v=v2&amp;amp;px=999" role="button" title="Annotation 2019-12-20 144523.png" alt="Annotation 2019-12-20 144523.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2019 19:48:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613312#M179103</guid>
      <dc:creator>ghosh</dc:creator>
      <dc:date>2019-12-20T19:48:41Z</dc:date>
    </item>
    <item>
      <title>Re: split sas values in two but keep the leading 0's and space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613314#M179104</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/304341"&gt;@Kp1234&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for your reply,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm using the proc import to read a text file which contains the variable&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC IMPORT OUT=manifest&lt;BR /&gt;datafile="../../sd/accumulative/manifests/manifestcomb.txt"&lt;BR /&gt;REPLACE ;&lt;BR /&gt;delimiter='09'x;&lt;BR /&gt;guessingrows=32767;&lt;BR /&gt;RUN;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;when sas reads the variable "Study Code"n (This is one variable), it takes out space and the leading 0's Eg:&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;0156 00062&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;becomes: 15600062,&lt;/P&gt;
&lt;P&gt;is there something that i need to do in the PROC import to keep the formatting?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Using PROC IMPORT will introduce some inconsistency in how the variables are defined since it can only guess what you want.&amp;nbsp; But it will NOT remove an embedded spaces.&amp;nbsp; Sounds like your text file does not have spaces between the two parts of the field. And since that makes it look like a numeric value PROC IMPORT will make a numeric variable instead of a character variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The real fix is to just write your own program to read the text file instead of using PROC IMPORT.&amp;nbsp; That will also allow you to fix the goofy variable name with spaces in the middle of it that was used in the code in the original question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you already have a numeric variable and you want it to print using nine digits then use the Z9. format.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format subject_code z9.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or use the Z9. format with a PUT() function call to make a character variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;subject_code_string=put(subject_code,z9.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have read actual read the two separate values as individual numeric variables and want to generate the 9 digit string then use the PUT() function on each number separately.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;subject_code=put(site,z4.)||put(subj,Z5.);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Dec 2019 19:51:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/split-sas-values-in-two-but-keep-the-leading-0-s-and-space/m-p/613314#M179104</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-20T19:51:09Z</dc:date>
    </item>
  </channel>
</rss>

