<?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 Insert a &amp;quot;-&amp;quot; in the subject ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559678#M156321</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a variable called subject_ID composed of Site ID (first one or two number are Site and the last 4 digits are subject ID) and&amp;nbsp; saved as text and I want to add a "-" in between, like&amp;nbsp;12-0006,&amp;nbsp;9-0011 or&amp;nbsp;22-0001 for example, any idea is greatly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Subject_ID&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;120006&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;120004&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;120003&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;120002&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;120005&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;90011&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;90010&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;90006&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;90001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;90009&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;220001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;220019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;220017&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Fri, 17 May 2019 15:04:11 GMT</pubDate>
    <dc:creator>zimcom</dc:creator>
    <dc:date>2019-05-17T15:04:11Z</dc:date>
    <item>
      <title>Insert a "-" in the subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559678#M156321</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a variable called subject_ID composed of Site ID (first one or two number are Site and the last 4 digits are subject ID) and&amp;nbsp; saved as text and I want to add a "-" in between, like&amp;nbsp;12-0006,&amp;nbsp;9-0011 or&amp;nbsp;22-0001 for example, any idea is greatly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Subject_ID&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;120006&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;120004&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;120003&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;120002&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;120005&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;90011&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;90010&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;90006&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;90001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;90009&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;220001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;220019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;220017&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 17 May 2019 15:04:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559678#M156321</guid>
      <dc:creator>zimcom</dc:creator>
      <dc:date>2019-05-17T15:04:11Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a "-" in the subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559679#M156322</link>
      <description>&lt;P&gt;Is Subject_ID a numeric or character variable?&lt;/P&gt;</description>
      <pubDate>Fri, 17 May 2019 15:07:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559679#M156322</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-05-17T15:07:13Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a "-" in the subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559681#M156323</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;SPAN&gt;character&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 17 May 2019 15:10:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559681#M156323</guid>
      <dc:creator>zimcom</dc:creator>
      <dc:date>2019-05-17T15:10:18Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a "-" in the subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559683#M156324</link>
      <description>&lt;P&gt;So is there a rule that would allow us to separate the cases where you have a single digit before the dash, and the cases where you have two digits before the dash?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 May 2019 15:12:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559683#M156324</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-05-17T15:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a "-" in the subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559684#M156325</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Subject_ID $;
cards;
120006
120004
120003
120002
120005
90011
90010
90006
90001
90009
220001
220019
220017
;

data want;
set have;
temp=indexc(Subject_ID,'0');
want=catx('-',substr(Subject_ID,1,temp-1),substr(subject_id,temp));
drop temp;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I made some silly assumptions though &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 May 2019 15:15:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559684#M156325</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-17T15:15:37Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a "-" in the subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559685#M156326</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;the last 4 digits are the patient ID, it always has 4 didgits, the first 1 or 2 digits are the site iD, when the sites run over 10, it becomes 2 digits. So basically it is to insert a "-" in the 5th place from the right, hope this is clear&lt;/P&gt;</description>
      <pubDate>Fri, 17 May 2019 15:16:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559685#M156326</guid>
      <dc:creator>zimcom</dc:creator>
      <dc:date>2019-05-17T15:16:01Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a "-" in the subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559686#M156327</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;That works if the 0 is the delimiter where the dash goes. Otherwise not.&lt;/P&gt;</description>
      <pubDate>Fri, 17 May 2019 15:16:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559686#M156327</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-05-17T15:16:18Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a "-" in the subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559688#M156329</link>
      <description>&lt;P&gt;If the variable is character what is the current defined length of the variable? What is the longest current value in the variable?&lt;/P&gt;
&lt;P&gt;If the two numbers above match then you will need to do something to increase the length of the variable because inserting would make the variable too long to fit in the current definition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;data want;
   length subject_id $ 7;
   set have;
   if length(subject_id)=6 then subject_id = catx('-',substr(subject_id,1,2),substr(subject_id,3) ;&lt;BR /&gt;   else if length(subject_id)=5 then subject_id = catx('-',substr(subject_id,1,1),substr(subject_id,2) ; 
run;&lt;/PRE&gt;
&lt;P&gt;I use 7 above as the longest value you showed was 6 characters. If the actual defined length is longer than the longest value the length statement would not be needed.&lt;/P&gt;
&lt;P&gt;Edited to address the 5 character length option I missed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your variable is NUMERIC then you will need to create an entirely new variable as you cannot have - as you would intend. I suspect I might be able to create a format that would display the current numeric as desired but that sounds fragile.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Who combined these values? For a large number of purposes it may be easier to use Site and the 4 digits (or other identifier) as separate variables (of course if they are already in the data let us know).&lt;/P&gt;</description>
      <pubDate>Fri, 17 May 2019 15:49:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559688#M156329</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-05-17T15:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a "-" in the subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559689#M156330</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/219549"&gt;@zimcom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;the last 4 digits are the patient ID, it always has 4 didgits, the first 1 or 2 digits are the site iD, when the sites run over 10, it becomes 2 digits. So basically it is to insert a "-" in the 5th place from the right, hope this is clear&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    length new_subject_id $ 8;
    len=length(subject_id);
    new_subject_id=catx('-',substr(subject_id,1,len-4),substr(subject_id,len-3));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 May 2019 15:20:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559689#M156330</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-05-17T15:20:08Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a "-" in the subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559691#M156332</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;Yes you are right.&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/219549"&gt;@zimcom&lt;/a&gt;&amp;nbsp; The following works with no assumptions&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Subject_ID $;
cards;
120006
120004
120003
120002
120005
90011
90010
90006
90001
90009
220001
220019
220017
;

data want;
set have;
temp=substr(Subject_ID,length(Subject_ID)-4+1,4);
temp1=substr(Subject_ID,1,length(Subject_ID)-4);
want=catx('-',temp1,temp);
drop temp:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 May 2019 15:22:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559691#M156332</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-17T15:22:54Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a "-" in the subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559699#M156337</link>
      <description>&lt;P&gt;The solution from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;fails for the case where the Subject_ID is 90011&lt;/P&gt;</description>
      <pubDate>Fri, 17 May 2019 15:34:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559699#M156337</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-05-17T15:34:34Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a "-" in the subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559872#M156413</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Subject_ID $;
length want $ 100;
want=prxchange('s/(\d{4})$/-$1/',1,strip(Subject_ID));
cards;
120006
120004
120003
120002
120005
90011
90010
90006
90001
90009
220001
220019
220017
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 18 May 2019 12:56:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559872#M156413</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-05-18T12:56:17Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a "-" in the subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559931#M156436</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/219549"&gt;@zimcom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or code as below. Eventually add some check on the length of the source string to deal with incomplete source data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(drop=_:);
  infile datalines truncover;
  input Subject_ID $;
  length want $ 100;
  _len=lengthn(Subject_ID);
  want=substrn(Subject_ID,1,_len-4)||'-'||substrn(Subject_ID,_len-3);
  cards;
87654321
7654321
654321
54321
4321
321
21
1
 
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 209px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/29629iED3332A4C3C86F18/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 19 May 2019 02:38:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-quot-quot-in-the-subject-ID/m-p/559931#M156436</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-05-19T02:38:12Z</dc:date>
    </item>
  </channel>
</rss>

