<?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: Separating a concatenated variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237608#M43555</link>
    <description>&lt;P&gt;You can use tranwrd() to remove the id from the concatenated string, then format accordingly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input ID $6. concatenated_var $15.;&lt;BR /&gt;cards;&lt;BR /&gt;12345&amp;nbsp; 1234512032015&lt;BR /&gt;123456 12345612032015&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;date = input(tranwrd(strip(concatenated_var),strip(id),trimn('')),mmddyy10.);&lt;BR /&gt;format date mmddyy10.;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Thu, 03 Dec 2015 16:02:23 GMT</pubDate>
    <dc:creator>Steelers_In_DC</dc:creator>
    <dc:date>2015-12-03T16:02:23Z</dc:date>
    <item>
      <title>Separating a concatenated variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237598#M43548</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with an ID variable, and a concatenation of the ID variable, a date variable, and a numeric variable. What I want is to take the date value out of the concatenated variable and format it as a date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;concatenated_var&lt;/P&gt;&lt;P&gt;12345 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12345184231&lt;/P&gt;&lt;P&gt;123456 &amp;nbsp; &amp;nbsp; &amp;nbsp; 12345618444123&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I tried was this:&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;date_var=input(substr(ID, 5, 5), 5.);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and then another step that formatted date_var as a date variable (for some reason when I tried to do that all in one step like this: &lt;SPAN&gt;date_var=input(substr(ID, 5, 5), mmddyy10.), every value of date_var was missing). It worked, but the problem is that I'm now finding that the ID variable has either 5 or 6 digits. The numeric variable at the end of the concatenated variable also has different lengths.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question is whether there's a way to extract the date part of the concatenated variable (possibly using the ID variable since I do have that one; I could probably bring the numeric one in but don't currently have it in my dataset).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is much appreciated.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 15:46:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237598#M43548</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2015-12-03T15:46:40Z</dc:date>
    </item>
    <item>
      <title>Re: Separating a concatenated variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237601#M43551</link>
      <description>Well, if you have the length of the ID you know how long to make your substring. What is that date supposed to represent though, mmdddyy - 18 42 31?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 03 Dec 2015 15:50:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237601#M43551</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-12-03T15:50:37Z</dc:date>
    </item>
    <item>
      <title>Re: Separating a concatenated variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237603#M43552</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For one thing, you don't want to substring ID, you want to substring concatenated_var.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Secondly, you can use the length of ID to determine where to start in concatenated_var (position 6 or 7), e.g.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date_var=input(substr(concatenated_var,length(ID)+1,5),5.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then, assuming date_var is constant length (5) you can get your numeric var in a similar fashion.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 15:54:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237603#M43552</guid>
      <dc:creator>JoshB</dc:creator>
      <dc:date>2015-12-03T15:54:58Z</dc:date>
    </item>
    <item>
      <title>Re: Separating a concatenated variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237604#M43553</link>
      <description>&lt;P&gt;The problem is that the ID is not always the same length--it is sometimes 5 and sometimes 6 digits.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The date part is 18423, the 1 is from the numeric variable which was also concatenated with the ID var and date var.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 15:53:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237604#M43553</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2015-12-03T15:53:13Z</dc:date>
    </item>
    <item>
      <title>Re: Separating a concatenated variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237608#M43555</link>
      <description>&lt;P&gt;You can use tranwrd() to remove the id from the concatenated string, then format accordingly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input ID $6. concatenated_var $15.;&lt;BR /&gt;cards;&lt;BR /&gt;12345&amp;nbsp; 1234512032015&lt;BR /&gt;123456 12345612032015&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;date = input(tranwrd(strip(concatenated_var),strip(id),trimn('')),mmddyy10.);&lt;BR /&gt;format date mmddyy10.;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 16:02:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237608#M43555</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2015-12-03T16:02:23Z</dc:date>
    </item>
    <item>
      <title>Re: Separating a concatenated variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237616#M43557</link>
      <description>&lt;P&gt;Understanding that all you need is to get the 5 digits after ID, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/50008"&gt;@JoshB﻿&lt;/a&gt;'s classic solution will get what you need.&amp;nbsp;&amp;nbsp;You need also to check the data, making sure all of the dates are 5 digits (eg. 09999 for 18May1987). By the same token, using PRX functions we can also have:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
	input (id date_var) (:$20.);
	cards;
12345         12345184231
123456       12345618444123
;

data want;
	set have;
	format dt date9.;
	dt=input(prxchange(cats('s/(',id,')(\d{5})(.*)/$2/'),-1,date_var),5.);
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 16:30:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237616#M43557</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2015-12-03T16:30:41Z</dc:date>
    </item>
    <item>
      <title>Re: Separating a concatenated variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237619#M43558</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30712"&gt;@Steelers_In_DC&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;You can use tranwrd() to remove the id from the concatenated string, then format accordingly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input ID $6. concatenated_var $15.;&lt;BR /&gt;cards;&lt;BR /&gt;12345&amp;nbsp; 1234512032015&lt;BR /&gt;123456 12345612032015&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;date = input(tranwrd(strip(concatenated_var),strip(id),trimn('')),mmddyy10.);&lt;BR /&gt;format date mmddyy10.;&lt;BR /&gt;run;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Two concerns regarding your approach,&lt;/P&gt;
&lt;P&gt;1. OP's "concatenated_var" has some other elements&amp;nbsp;besides ID and date.&lt;/P&gt;
&lt;P&gt;2. What if&amp;nbsp;id and date take the same value or their combination generate the same value? id=12345 concatenated_var=1234512345 // id=12312 concatenated_var=1231231221?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 16:38:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237619#M43558</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2015-12-03T16:38:59Z</dc:date>
    </item>
    <item>
      <title>Re: Separating a concatenated variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237621#M43560</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30712"&gt;@Steelers_In_DC﻿&lt;/a&gt;: I'm sorry, but I think you've overlooked three things:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;There is an additional "numeric" value at the end of &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37814"&gt;@Walternate﻿&lt;/a&gt;'s&amp;nbsp;concatenated_var.&lt;/LI&gt;
&lt;LI&gt;The date values embedded in&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37814"&gt;@Walternate﻿&lt;/a&gt;&lt;SPAN&gt;'s&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;concatenated_var appear to be SAS date values, but at least not MMDDYY values (although, I must say, I have never come across such data in the past&amp;nbsp;18 years).&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;Most importantly, I would not take the risk of deleting or replacing substrings in a concatenated string, if there was uncertainty as to &lt;EM&gt;where&lt;/EM&gt; the pattern match would occur. In the worst case, it could even occur twice. Example: ID='84231', CONCATENATED_VAR='84231184231' (including the same "date" value 18423 as in the original sample data). TRANWRD would turn this into a single '1'.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 16:42:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237621#M43560</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2015-12-03T16:42:22Z</dc:date>
    </item>
    <item>
      <title>Re: Separating a concatenated variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237637#M43563</link>
      <description>&lt;P&gt;I did miss the trailing numbers so a substr() would be necessary:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;format date mmddyy10.;&lt;BR /&gt;set have;&lt;BR /&gt;test = put(input(substr(strip(tranwrd(strip(concatenated_var),strip(id),trimn(''))),1,5),8.),mmddyy10.);&lt;BR /&gt;date = input(test,mmddyy10.);&lt;BR /&gt;drop test;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there are more dates or other numbers placed throughout the string I agree this would not be a good solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 17:49:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-a-concatenated-variable/m-p/237637#M43563</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2015-12-03T17:49:43Z</dc:date>
    </item>
  </channel>
</rss>

