<?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 Delete a string (in variable) from a character string(in variable) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Delete-a-string-in-variable-from-a-character-string-in-variable/m-p/265038#M52049</link>
    <description>&lt;P&gt;Hello All,&lt;/P&gt;
&lt;P&gt;I am trying to create a list where a certain string is deleted from a string list.&lt;/P&gt;
&lt;P&gt;for example: considering the third row in the table below where year=2005,2007, the value 2005,2007 should be deleted from year_list and&amp;nbsp;with end result being "2006,2008,2009,2010,2011"&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The original dataset looks like this:&lt;/P&gt;
&lt;TABLE width="302"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="70"&gt;year&lt;/TD&gt;
&lt;TD width="232"&gt;year_list&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2005&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2005,2007&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2008,2011&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;PRE&gt;proc sql;
create table have (year VARCHAR(1000), year_list VARCHAR(1000));
insert into have values('2005','2005,2006,2007,2008,2009,2010,2011');
insert into have values('2005,2007' '2005,2006,2007,2008,2009,2010,2011');
insert into have values('2008,2011' '2005,2006,2007,2008,2009,2010,2011');
run;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "want" variable is what i require as output - as shown in the following table (with code below):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="501"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="70"&gt;year&lt;/TD&gt;
&lt;TD width="232"&gt;year_list&lt;/TD&gt;
&lt;TD width="199"&gt;want&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2005&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;TD&gt;2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2005,2007&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;TD&gt;2006,2008,2009,2010,2012&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2008,2011&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2009,2010&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;PRE&gt;proc sql;
create table want (year VARCHAR(1000), year_list VARCHAR(1000), want_list VARCHAR(1000));
insert into want values('2005','2005,2006,2007,2008,2009,2010,2011','2006,2007,2008,2009,2010,2011');
insert into want values('2005,2007' '2005,2006,2007,2008,2009,2010,2011','2006,2008,2009,2010,2012');
insert into want values('2008,2011' '2005,2006,2007,2008,2009,2010,2011','2005,2006,2007,2009,2010');
run;&lt;/PRE&gt;
&lt;P&gt;Any help would be appreciated.&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
    <pubDate>Wed, 20 Apr 2016 09:27:53 GMT</pubDate>
    <dc:creator>sebster24</dc:creator>
    <dc:date>2016-04-20T09:27:53Z</dc:date>
    <item>
      <title>Delete a string (in variable) from a character string(in variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Delete-a-string-in-variable-from-a-character-string-in-variable/m-p/265038#M52049</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;
&lt;P&gt;I am trying to create a list where a certain string is deleted from a string list.&lt;/P&gt;
&lt;P&gt;for example: considering the third row in the table below where year=2005,2007, the value 2005,2007 should be deleted from year_list and&amp;nbsp;with end result being "2006,2008,2009,2010,2011"&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The original dataset looks like this:&lt;/P&gt;
&lt;TABLE width="302"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="70"&gt;year&lt;/TD&gt;
&lt;TD width="232"&gt;year_list&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2005&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2005,2007&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2008,2011&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;PRE&gt;proc sql;
create table have (year VARCHAR(1000), year_list VARCHAR(1000));
insert into have values('2005','2005,2006,2007,2008,2009,2010,2011');
insert into have values('2005,2007' '2005,2006,2007,2008,2009,2010,2011');
insert into have values('2008,2011' '2005,2006,2007,2008,2009,2010,2011');
run;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "want" variable is what i require as output - as shown in the following table (with code below):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="501"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="70"&gt;year&lt;/TD&gt;
&lt;TD width="232"&gt;year_list&lt;/TD&gt;
&lt;TD width="199"&gt;want&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2005&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;TD&gt;2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2005,2007&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;TD&gt;2006,2008,2009,2010,2012&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2008,2011&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2009,2010&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;PRE&gt;proc sql;
create table want (year VARCHAR(1000), year_list VARCHAR(1000), want_list VARCHAR(1000));
insert into want values('2005','2005,2006,2007,2008,2009,2010,2011','2006,2007,2008,2009,2010,2011');
insert into want values('2005,2007' '2005,2006,2007,2008,2009,2010,2011','2006,2008,2009,2010,2012');
insert into want values('2008,2011' '2005,2006,2007,2008,2009,2010,2011','2005,2006,2007,2009,2010');
run;&lt;/PRE&gt;
&lt;P&gt;Any help would be appreciated.&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Apr 2016 09:27:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Delete-a-string-in-variable-from-a-character-string-in-variable/m-p/265038#M52049</guid>
      <dc:creator>sebster24</dc:creator>
      <dc:date>2016-04-20T09:27:53Z</dc:date>
    </item>
    <item>
      <title>Re: Delete a string (in variable) from a character string(in variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Delete-a-string-in-variable-from-a-character-string-in-variable/m-p/265043#M52051</link>
      <description>&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;format want_list $1000.;&lt;BR /&gt;nyear=countw(year,',');&lt;BR /&gt;do i=1 to nyear;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;rmyear=scan(year,i,',');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; want_list=prxchange(cats("s/",rmyear,",*//"),-1,year_list);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/* Remove last commas */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; want_list=prxchange(cats("s/, *$//"),-1,want_list);&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;drop i rmyear nyear;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Apr 2016 09:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Delete-a-string-in-variable-from-a-character-string-in-variable/m-p/265043#M52051</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-04-20T09:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: Delete a string (in variable) from a character string(in variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Delete-a-string-in-variable-from-a-character-string-in-variable/m-p/265047#M52052</link>
      <description>&lt;P&gt;Whilst you have a solution given, I would question the logic of having a series of data items, comma separated, in one variable. &amp;nbsp;You are fighting the basic strcuture of SAS data doing so. &amp;nbsp;SAS strcuture is one column per data item, and procedures and functions are based on this principal. &amp;nbsp;Any furhter processing you do on that string will involve far more processing than is necessary. &amp;nbsp;I would advise you to re-think it, even at worse case scenario, just have a bunch of varx - vary variables:&lt;/P&gt;
&lt;P&gt;YEAR &amp;nbsp; &amp;nbsp; VAR1 &amp;nbsp; &amp;nbsp;VAR2 &amp;nbsp; VAR3 ...&lt;/P&gt;
&lt;P&gt;2005 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2005 &amp;nbsp; &amp;nbsp; 2006 &amp;nbsp; 2007&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Think about how much simpler it would be being able to use array processing on the above using of var:&lt;/P&gt;
&lt;P&gt;And if you want a string somewhere in the line then:&lt;/P&gt;
&lt;P&gt;catx(",",of var:);&lt;/P&gt;
&lt;P&gt;Will do the trick no problem. &amp;nbsp;Make your likfe easier, that is what us lazy programmers do&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Apr 2016 10:08:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Delete-a-string-in-variable-from-a-character-string-in-variable/m-p/265047#M52052</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-04-20T10:08:01Z</dc:date>
    </item>
    <item>
      <title>Re: Delete a string (in variable) from a character string(in variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Delete-a-string-in-variable-from-a-character-string-in-variable/m-p/265095#M52072</link>
      <description>&lt;P&gt;Hello RW9,&lt;/P&gt;
&lt;P&gt;Thank you for your suggestion. I followed what you mentioned and split the data into different variables (coded below)&lt;/P&gt;
&lt;P&gt;However, I dont know how to approach this problem with listing as mentioned earlier.&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table have (year VARCHAR(100), year_list VARCHAR(100));
insert into have values('2005','2005,2006,2007,2008,2009,2010,2011');
insert into have values('2005,2007','2005,2006,2007,2008,2009,2010,2011');
insert into have values('2007,2011','2005,2006,2007,2008,2009,2010,2011');
run;

data tmp1;
set have;
csv=countw(year,",");
codgen_csv=countw(year_list,",");
run;


proc sql ;
select distinct cats("mart",max(csv)), max(csv) into :arraylist,:arraycnt from tmp1;
select distinct cats("dlv",max(codgen_csv)), max(codgen_csv) into :dlv_array, :dlvcnt from tmp1;
run;

data want (drop=list);
set tmp1;
length mart1-&amp;amp;arraylist. $100.;
length dlv1-&amp;amp;dlv_array. $100.;
array mart(&amp;amp;arraycnt.) $;
array dlv(&amp;amp;dlvcnt.) $;
do _i=1 to dim(mart);
mart[_i]=scan(year,_i,',');
end;
do _j=1 to dim(dlv);
dlv[_j]=scan(year_list,_j,',');
end;
run;

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Also, i forgot to mention this in the previous message that the lower bound of the dataset changes: i.e. the last row of the following table, "want" column, notes the first observation as&amp;nbsp;2009, as&amp;nbsp;variable&amp;nbsp;year begins with 2008. In other words, the first variable mentioned in year acts as the starting point. The end point the last year mentioned in year_list.&lt;/P&gt;
&lt;P&gt;The final result should like this:&lt;/P&gt;
&lt;TABLE width="501"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="70"&gt;year&lt;/TD&gt;
&lt;TD width="232"&gt;year_list&lt;/TD&gt;
&lt;TD width="199"&gt;want&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2005&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;TD&gt;2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2005,2007&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;TD&gt;2006,2008,2009,2010,2012&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2008,2011&lt;/TD&gt;
&lt;TD&gt;2005,2006,2007,2008,2009,2010,2011&lt;/TD&gt;
&lt;TD&gt;2009,2010&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;Please do let me know.&lt;/P&gt;
&lt;P&gt;Thank you for your time.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Apr 2016 13:43:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Delete-a-string-in-variable-from-a-character-string-in-variable/m-p/265095#M52072</guid>
      <dc:creator>sebster24</dc:creator>
      <dc:date>2016-04-20T13:43:34Z</dc:date>
    </item>
    <item>
      <title>Re: Delete a string (in variable) from a character string(in variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Delete-a-string-in-variable-from-a-character-string-in-variable/m-p/265348#M52175</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is unclearn, at leat to me, what You are trying to achieve.&lt;/P&gt;
&lt;P&gt;As RW9 said, it could be useful to take some time to think about&lt;/P&gt;
&lt;P&gt;which form should take your data to make your work simpler.&lt;/P&gt;
&lt;P&gt;Since we are dealing with years, Yu'd probably prefer using&lt;/P&gt;
&lt;P&gt;numeric variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For instance, year_list seems identical for all observations and contains&lt;/P&gt;
&lt;P&gt;consecutive years. So all you need is the first and the last year.&lt;/P&gt;
&lt;P&gt;You can store them in macro-variables to avoid repeating them on each observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does the following code answer your problem ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;create table have (year VARCHAR(100), year_list VARCHAR(100));&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;insert into have values('2005','2005,2006,2007,2008,2009,2010,2011');&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;insert into have values('2005,2007','2005,2006,2007,2008,2009,2010,2011');&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;insert into have values('2007,2011','2005,2006,2007,2008,2009,2010,2011');&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%let period_start=2005;&lt;BR /&gt;%let period_end=2011;&lt;BR /&gt;&lt;BR /&gt;data do_you_want_this;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;set have;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;format start end 4.&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; want $1000.;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;start = max(put(scan(year,1,','),4.)+1,&amp;amp;period_start.);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;end = &amp;amp;period_end.-1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;exclude = put(scan(year,2,','),4.);&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;do i=start to end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if i ne exclude then want=catx(',',want,i);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;end;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;drop exclude i;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Apr 2016 08:14:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Delete-a-string-in-variable-from-a-character-string-in-variable/m-p/265348#M52175</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-04-21T08:14:44Z</dc:date>
    </item>
    <item>
      <title>Re: Delete a string (in variable) from a character string(in variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Delete-a-string-in-variable-from-a-character-string-in-variable/m-p/265376#M52182</link>
      <description>&lt;P&gt;True. However, the complexity here is that the years mentioned here is not in numeric format, but in character form. i.e. the year could have a suffix/ prefix . for example: M2010_SA, M8_2009.&lt;/P&gt;
&lt;P&gt;Unfortunately, this is the data i receive, and although i would like to change the data composition to match the year format, this would be a huge hassle&amp;nbsp;as it&amp;nbsp;would require changing many more parameters in many scripts.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Apr 2016 10:23:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Delete-a-string-in-variable-from-a-character-string-in-variable/m-p/265376#M52182</guid>
      <dc:creator>sebster24</dc:creator>
      <dc:date>2016-04-21T10:23:31Z</dc:date>
    </item>
  </channel>
</rss>

