<?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 IF char column contain in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/IF-char-column-contain/m-p/759662#M240115</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have a data set with 2 columns called ID and Vector.&lt;/P&gt;
&lt;P&gt;Column "Vector" including concatenation of codes .&lt;/P&gt;
&lt;P&gt;I want to create a new data set called wanted with a new column called "Ind" that classify each ID into one of two groups: Red or Black.&lt;/P&gt;
&lt;P&gt;If field "Vector" contain at least one of the codes in macro variable&amp;nbsp;&lt;CODE class=" language-sas"&gt;black_list then&amp;nbsp;Ind='Black'&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;else&amp;nbsp;Ind='Green'&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;I also want to create new columns with binary values 1/0 of splitting column Vector.&lt;/P&gt;
&lt;P&gt;The new columns names will be :Code_23, Code_48,Code_65 and so on.&lt;/P&gt;
&lt;P&gt;The order of the columns will be by the numbers order (3,6,12,13&amp;nbsp; and so on)&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;What&amp;nbsp;is&amp;nbsp;the&amp;nbsp;way&amp;nbsp;to&amp;nbsp;do&amp;nbsp;it&amp;nbsp;please?&lt;/CODE&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Input ID Vector $20.;
Cards;
1 23,48,65,19,82,17,15
2 16
3 98,76,13,19,25
4 87,12,52,76,24
5 3,28
6 6,16
7 19,82,25
8 28,98,26
9 76,43,25
10 43,65,87,98,12
;
Run;


%let black_list=25,19,18;
 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 05 Aug 2021 10:43:06 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2021-08-05T10:43:06Z</dc:date>
    <item>
      <title>IF char column contain</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-char-column-contain/m-p/759662#M240115</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have a data set with 2 columns called ID and Vector.&lt;/P&gt;
&lt;P&gt;Column "Vector" including concatenation of codes .&lt;/P&gt;
&lt;P&gt;I want to create a new data set called wanted with a new column called "Ind" that classify each ID into one of two groups: Red or Black.&lt;/P&gt;
&lt;P&gt;If field "Vector" contain at least one of the codes in macro variable&amp;nbsp;&lt;CODE class=" language-sas"&gt;black_list then&amp;nbsp;Ind='Black'&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;else&amp;nbsp;Ind='Green'&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;I also want to create new columns with binary values 1/0 of splitting column Vector.&lt;/P&gt;
&lt;P&gt;The new columns names will be :Code_23, Code_48,Code_65 and so on.&lt;/P&gt;
&lt;P&gt;The order of the columns will be by the numbers order (3,6,12,13&amp;nbsp; and so on)&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;What&amp;nbsp;is&amp;nbsp;the&amp;nbsp;way&amp;nbsp;to&amp;nbsp;do&amp;nbsp;it&amp;nbsp;please?&lt;/CODE&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Input ID Vector $20.;
Cards;
1 23,48,65,19,82,17,15
2 16
3 98,76,13,19,25
4 87,12,52,76,24
5 3,28
6 6,16
7 19,82,25
8 28,98,26
9 76,43,25
10 43,65,87,98,12
;
Run;


%let black_list=25,19,18;
 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Aug 2021 10:43:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-char-column-contain/m-p/759662#M240115</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-08-05T10:43:06Z</dc:date>
    </item>
    <item>
      <title>Re: IF char column contain</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-char-column-contain/m-p/759667#M240118</link>
      <description>&lt;P&gt;For the first, do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Input ID Vector $20.;
Cards;
1 23,48,65,19,82,17,15
2 16
3 98,76,13,19,25
4 87,12,52,76,24
5 3,28
6 6,16
7 19,82,25
8 28,98,26
9 76,43,25
10 43,65,87,98,12
;

%let black_list=25,19,18;

data green_black;
set have;
ind = "green";
do i = 1 to countw("&amp;amp;black_list.",",");
  if index(vector,scan("&amp;amp;black_list.",i,",")) then ind = "black";
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For the second, transpose to long, create a sorted template, and transpose back to wide:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data long;
set have;
length code $7;
value = 1;
do i = 1 to countw(vector,",");
  code = "Code_" !! scan(vector,i,",");
  output;
end;
drop i vector;
run;

proc sort data=long (keep=code) out=template nodup;
by code;
run;

data long2;
set
  template
  long
;
run;

proc transpose data=long out=wide (drop=_: where=(id ne .));
by id;
var value;
id code;
run;

data want;
set wide;
array nums _numeric_;
do over nums;
  nums = coalesce(nums,0);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Aug 2021 11:56:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-char-column-contain/m-p/759667#M240118</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-08-05T11:56:57Z</dc:date>
    </item>
    <item>
      <title>Re: IF char column contain</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-char-column-contain/m-p/759693#M240126</link>
      <description>&lt;P&gt;Consider the advantages of creating variables for all 99 codes.&amp;nbsp; (You can adjust this if there are more than 99 possible codes.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The programming will be easier.&amp;nbsp; If you have multiple batches of data, they will all contain the same variables.&amp;nbsp; And if you really want to eliminate codes which do not appear in the data, you can always do that later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the idea:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   array codes {99} code_1 - code_99;
   do _n_=1 to 99;
       codes{_n_} = 0;
   end;
   do _n_=1 to countw(vector, ',');
      codes{input(scan(vector, _n_, ','), 2.)} = 1;
   end;
   ind='green';
   do _n_=&amp;amp;blacklist;
      if codes{_n_}=1 then ind='black';
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you're done ... both objectives ... unless you still want to remove codes that are always 0.&amp;nbsp; If so, continue with:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=want;
   var code_1 - code_99;
   output out=stats sum=;
run;
data _null_;
   set stats;
   length droplist $ 1000;
   array codes {99} code_1-code_99;
   do _n_=1 to 99;
      if codes{_n_}=0 then droplist = catt (droplist, vname(codes{_n_}));
   end;
   call symputx('droplist', droplist);
end;
data really_want;
   set want (drop=(&amp;amp;droplist));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code is all untested, so write back if you encounter any difficulty with testing and debugging.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Aug 2021 13:51:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-char-column-contain/m-p/759693#M240126</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-08-05T13:51:47Z</dc:date>
    </item>
  </channel>
</rss>

