<?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: Data Step: Removing and Retaining a Value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/693089#M211285</link>
    <description>&lt;P&gt;OK, that was the old code.&lt;/P&gt;
&lt;P&gt;See this restructured code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length jobs $ 100 job $5;
input jobs $;
n = _N_;
datalines;
11,2,11,2
11,2
2,11,2
2,11,3,11,4,11
;

data int1;
set have;
do i = 1 to countw(jobs,',');
  job = scan(jobs,i,',');
  output;
end;
keep n job;
run;

proc sort
  data=int1
  dupout=int2
  nodupkey
;
by n job;
run;

proc sort
  data=int2
  nodupkey
;
by n job;
run;

data want;
merge
  int2
  have (keep=n)
;
by n;
length jobs $100;
retain jobs;
if first.n
then jobs = job;
else jobs = catx(',',jobs,job);
if last.n;
drop job;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;n	jobs
1	11,2
2	 
3	2
4	11
&lt;/PRE&gt;</description>
    <pubDate>Wed, 21 Oct 2020 08:48:22 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-10-21T08:48:22Z</dc:date>
    <item>
      <title>Data Step: Removing and Retaining a Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/692938#M211195</link>
      <description>&lt;P&gt;data have;&lt;BR /&gt;length jobs $ 100;&lt;BR /&gt;input jobs $;&lt;BR /&gt;datalines;&lt;BR /&gt;11,2,11,2&lt;BR /&gt;11,2&lt;BR /&gt;2,11,2&lt;BR /&gt;2,11,3,11,4,11&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From the dataset given, May I know how to add a column that has this logic:&lt;/P&gt;&lt;P&gt;1. If a number appeared only once, it will be removed&lt;/P&gt;&lt;P&gt;2. If a number appeared more than once, it will be retained BUT it will only appear once&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To explain further, the output of the added column should be like this:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data want;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;length final $ 100;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;input final $;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;datalines;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;11,2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;11&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 16:52:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/692938#M211195</guid>
      <dc:creator>iSAS</dc:creator>
      <dc:date>2020-10-20T16:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: Data Step: Removing and Retaining a Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/692943#M211198</link>
      <description>Please show the exact expected output for this input data set.</description>
      <pubDate>Tue, 20 Oct 2020 16:18:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/692943#M211198</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-10-20T16:18:28Z</dc:date>
    </item>
    <item>
      <title>Re: Data Step: Removing and Retaining a Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/692944#M211199</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length jobs $ 100 job $5;
input jobs $;
n = _N_;
do i = 1 to countw(jobs,',');
  job = scan(jobs,i,',');
  output;
end;
keep n job;
datalines;
11,2,11,2
11,2
2,11,2
2,11,3,11,4,11
;

proc sort
  data=have
  dupout=intermediate
  nodupkey
;
by n job;
run;

proc sort
  data=intermediate
  nodupkey
;
by n job;
run;

data want;
set intermediate;
by n;
length jobs $100;
retain jobs;
if first.n
then jobs = job;
else jobs = catx(',',jobs,job);
if last.n;
drop job;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can join the result back to have by n.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 16:23:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/692944#M211199</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-20T16:23:08Z</dc:date>
    </item>
    <item>
      <title>Re: Data Step: Removing and Retaining a Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/692946#M211201</link>
      <description>&lt;P&gt;The expected output should be like this:&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;length final $ 100;&lt;BR /&gt;input final $;&lt;BR /&gt;datalines;&lt;BR /&gt;11,2&lt;BR /&gt;.&lt;BR /&gt;2&lt;BR /&gt;11&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 16:30:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/692946#M211201</guid>
      <dc:creator>iSAS</dc:creator>
      <dc:date>2020-10-20T16:30:28Z</dc:date>
    </item>
    <item>
      <title>Re: Data Step: Removing and Retaining a Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/692949#M211203</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length jobs $ 100 job $5;
input jobs $;
n = _N_;
do i = 1 to countw(jobs,',');
  job = scan(jobs,i,',');
  output;
end;
keep n job;
datalines;
11,2,11,2
11,2
2,11,2
2,11,3,11,4,11
;

proc sort
  data=have
  dupout=intermediate
  nodupkey
;
by n job;
run;

proc sort
  data=intermediate
  nodupkey
;
by n job;
run;

data want;
set intermediate;
by n;
length jobs $100;
retain jobs;
if first.n
then jobs = job;
else jobs = catx(',',jobs,job);
if last.n;
drop job;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Although I would recommend to keep the long dataset layout instead.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 16:40:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/692949#M211203</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-20T16:40:46Z</dc:date>
    </item>
    <item>
      <title>Re: Data Step: Removing and Retaining a Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/692963#M211215</link>
      <description>Hello KurtBremser, thank you for the reply.&lt;BR /&gt;In the final dataset work.want, the second observation of work.have was removed. What if, though the value is blank, I still want it to appear it work.want? What should be done if it should appear like this?:&lt;BR /&gt;data want;&lt;BR /&gt;length final $ 100;&lt;BR /&gt;input final $;&lt;BR /&gt;datalines;&lt;BR /&gt;11,2&lt;BR /&gt;.&lt;BR /&gt;2&lt;BR /&gt;11&lt;BR /&gt;;&lt;BR /&gt;run;</description>
      <pubDate>Tue, 20 Oct 2020 17:27:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/692963#M211215</guid>
      <dc:creator>iSAS</dc:creator>
      <dc:date>2020-10-20T17:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: Data Step: Removing and Retaining a Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/693089#M211285</link>
      <description>&lt;P&gt;OK, that was the old code.&lt;/P&gt;
&lt;P&gt;See this restructured code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length jobs $ 100 job $5;
input jobs $;
n = _N_;
datalines;
11,2,11,2
11,2
2,11,2
2,11,3,11,4,11
;

data int1;
set have;
do i = 1 to countw(jobs,',');
  job = scan(jobs,i,',');
  output;
end;
keep n job;
run;

proc sort
  data=int1
  dupout=int2
  nodupkey
;
by n job;
run;

proc sort
  data=int2
  nodupkey
;
by n job;
run;

data want;
merge
  int2
  have (keep=n)
;
by n;
length jobs $100;
retain jobs;
if first.n
then jobs = job;
else jobs = catx(',',jobs,job);
if last.n;
drop job;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;n	jobs
1	11,2
2	 
3	2
4	11
&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Oct 2020 08:48:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/693089#M211285</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-21T08:48:22Z</dc:date>
    </item>
    <item>
      <title>Re: Data Step: Removing and Retaining a Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/693157#M211321</link>
      <description>&lt;PRE&gt;data have;
input jobs $40.;
n = _N_;
do i = 1 to countw(jobs,',');
  job = scan(jobs,i,',');
  output;
end;
keep n job;
cards;
11,2,11,2
11,2
2,11,2
2,11,3,11,4,11
;
proc freq data=have noprint;
table n*job/out=freq list;
run;
data freq;
 set freq;
 if count=1 then call missing(job);
run;
data want;
 do until(last.n);
   set freq;
   by n;
   length jobs $ 200;
   jobs=catx(',',jobs,job);
 end;
 keep n jobs;
 run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Oct 2020 13:04:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Step-Removing-and-Retaining-a-Value/m-p/693157#M211321</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-10-21T13:04:01Z</dc:date>
    </item>
  </channel>
</rss>

