<?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: create new columns based on array values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911853#M359531</link>
    <description>&lt;P&gt;A very similar question was asked &lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-convert-character-variable-to-binary-value/m-p/911848#M359529" target="_self"&gt;here&lt;/A&gt;. I am skeptical that this re-arrangement actually improves anything. Can you please tell us what you plan to do next with this data? Most of the time (like almost all of the time), this wide format that you want is harder to work in SAS with than the long format I propose. So really, do tell us what is next.&lt;/P&gt;</description>
    <pubDate>Wed, 17 Jan 2024 19:26:51 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2024-01-17T19:26:51Z</dc:date>
    <item>
      <title>create new columns based on array values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911841#M359524</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset which resembles the table below&lt;/P&gt;
&lt;TABLE border="1" width="39.15353745541022%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="14.285714285714286%"&gt;id&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;cd_1&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;cd_2&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;cd_3&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;cd_4&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;cd_5&lt;/TD&gt;
&lt;TD width="1.1890606420927465%"&gt;cd_6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="14.285714285714286%"&gt;1234&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;AB01&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;AX01&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;AZ01&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="1.1890606420927465%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="14.285714285714286%"&gt;1236&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;AY01&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;AB01&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="1.1890606420927465%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="14.285714285714286%"&gt;1239&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;AF01&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;AG01&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;XZ01&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;AA01&lt;/TD&gt;
&lt;TD width="14.285714285714286%"&gt;AX01&lt;/TD&gt;
&lt;TD width="1.1890606420927465%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want my code to add additional columns based on the values above so it looks something like the below&lt;/P&gt;
&lt;TABLE border="1" width="62.901307966706305%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="6.9084423305588585%" height="30px"&gt;id&lt;/TD&gt;
&lt;TD width="6.195005945303209%" height="30px"&gt;AB01&lt;/TD&gt;
&lt;TD width="6.432818073721762%" height="30px"&gt;AX01&lt;/TD&gt;
&lt;TD width="6.076099881093935%" height="30px"&gt;AZ01&lt;/TD&gt;
&lt;TD width="5.362663495838288%" height="30px"&gt;AY01&lt;/TD&gt;
&lt;TD width="5.957193816884661%" height="30px"&gt;FB01&lt;/TD&gt;
&lt;TD width="6.432818073721762%" height="30px"&gt;AF01&lt;/TD&gt;
&lt;TD width="6.551724137931036%" height="30px"&gt;AG01&lt;/TD&gt;
&lt;TD width="6.432818073721762%" height="30px"&gt;XZ01&lt;/TD&gt;
&lt;TD width="6.551724137931039%" height="30px"&gt;AA01&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="6.9084423305588585%" height="30px"&gt;1234&lt;/TD&gt;
&lt;TD width="6.195005945303209%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="6.432818073721762%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="6.076099881093935%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="5.362663495838288%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="5.957193816884661%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="6.432818073721762%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="6.551724137931036%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="6.432818073721762%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="6.551724137931039%" height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="6.9084423305588585%" height="30px"&gt;1236&lt;/TD&gt;
&lt;TD width="6.195005945303209%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="6.432818073721762%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="6.076099881093935%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="5.362663495838288%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="5.957193816884661%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="6.432818073721762%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="6.551724137931036%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="6.432818073721762%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="6.551724137931039%" height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="6.9084423305588585%" height="30px"&gt;1239&lt;/TD&gt;
&lt;TD width="6.195005945303209%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="6.432818073721762%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="6.076099881093935%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="5.362663495838288%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="5.957193816884661%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="6.432818073721762%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="6.551724137931036%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="6.432818073721762%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="6.551724137931039%" height="30px"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have created an array from columns cd_1-cd_99 but have no idea on which functions I would need to use to determine the value in the array position and then create a new column where the name is the array value and the contents is either 1 or 0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to avoid listing out all of the possible values for cd_1-cd_99 as there are around 30 but these will occasionally change&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help would be greatly appreciated&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2024 18:04:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911841#M359524</guid>
      <dc:creator>twenty7</dc:creator>
      <dc:date>2024-01-17T18:04:40Z</dc:date>
    </item>
    <item>
      <title>Re: create new columns based on array values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911853#M359531</link>
      <description>&lt;P&gt;A very similar question was asked &lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-convert-character-variable-to-binary-value/m-p/911848#M359529" target="_self"&gt;here&lt;/A&gt;. I am skeptical that this re-arrangement actually improves anything. Can you please tell us what you plan to do next with this data? Most of the time (like almost all of the time), this wide format that you want is harder to work in SAS with than the long format I propose. So really, do tell us what is next.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2024 19:26:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911853#M359531</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-01-17T19:26:51Z</dc:date>
    </item>
    <item>
      <title>Re: create new columns based on array values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911870#M359535</link>
      <description>&lt;P&gt;whilst I do accept that the wider format is more difficult to work with it is needed in this instance as the data is to be exported and shared with non-technical users who need to be able to easily identify which records have a particular code&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2024 21:08:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911870#M359535</guid>
      <dc:creator>twenty7</dc:creator>
      <dc:date>2024-01-17T21:08:26Z</dc:date>
    </item>
    <item>
      <title>Re: create new columns based on array values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911880#M359538</link>
      <description>&lt;P&gt;Did you look at the link to the other EXTREMELY similar post that &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt; posted?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which is so similar it appears as if this is related to a homework assignment.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2024 23:34:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911880#M359538</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-01-17T23:34:58Z</dc:date>
    </item>
    <item>
      <title>Re: create new columns based on array values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911883#M359539</link>
      <description>&lt;P&gt;Below one way to go. Ideally your codes comply with SAS naming conventions for SAS variables.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* source data */
data have;
  infile datalines truncover dsd dlm=' ';
  input (id cd_1 cd_2 cd_3 cd_4 cd_5 cd_6) ($);
  some_other_var='ABC';
  datalines;
1234 AB01 AX01 AZ01   
1236 AY01 AB01    
1239 AF01 AG01 XZ01 AA01 AX01 
1999 $a#4
;

/* create table with distinct codes from all source variables */
data _null_;
  if _n_=1 then
    do;
      length varname $32;
      dcl hash h1(ordered:'y');
      h1.defineKey('varname');
      h1.defineData('varname');
      h1.defineDone();
    end;
  set have end=last;
  array vars{*} cd_:;
  do i=1 to dim(vars);
    varname=upcase(vars[i]);
    if not missing(varname) then h1.ref();
  end;
  if last then h1.output(dataset:'varnames');
run;

/* prepare data for 
   - creation of macro variable with desired code based variable names
   - informat that maps variable names to position (number) in array defined using above macro variable
*/
data varnames;
  set varnames;
  retain fmtname 'varpos' type 'i';
  start=varname;
  label=_n_;
run;

/* populate macro variable with distinct list of existing codes */
proc sql noprint;
  select cats("'",varname,"'n") into :varlist separated by ' '
  from varnames
  order by label
  ;
quit;

/* create informat to retrieve position (number) of word in variable list */
proc format cntlin=varnames;
run;

/* create want table */
data want;
  set have;
  array src_vars{*} cd_:;
  array trg_vars{*} 3 &amp;amp;varlist;
  do i=1 to dim(trg_vars);
    trg_vars[i]=0;
  end;
  do i=1 to dim(src_vars);
    if missing(src_vars[i]) then continue;
    trg_vars[input(upcase(src_vars[i]),varpos.)] = not missing(src_vars[i]);
  end;
  drop cd_: i;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1705535117823.png" style="width: 633px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92621iF237DBB1B356FE18/image-dimensions/633x136?v=v2" width="633" height="136" role="button" title="Patrick_0-1705535117823.png" alt="Patrick_0-1705535117823.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2024 23:45:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911883#M359539</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-01-17T23:45:27Z</dc:date>
    </item>
    <item>
      <title>Re: create new columns based on array values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911900#M359549</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dsd dlm=' ';
  input (id cd_1 cd_2 cd_3 cd_4 cd_5 cd_6) ($);
  datalines;
1234 AB01 AX01 AZ01   
1236 AY01 AB01    
1239 AF01 AG01 XZ01 AA01 AX01 
;

proc sort data=have out=temp;
by id;
run;
proc transpose data=temp out=temp2;
by id;
var cd_:;
run;
data temp3;
 set temp2(where=(col1 is not missing));
 v=1;
run;
proc transpose data=temp3 out=temp4;
by id;
var v;
id col1;
run;
proc stdize data=temp4(drop=_NAME_) out=want reponly missing=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Jan 2024 02:08:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911900#M359549</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-01-18T02:08:29Z</dc:date>
    </item>
    <item>
      <title>Re: create new columns based on array values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911934#M359560</link>
      <description>I did, however my understanding is that this solution relies on knowing all of the potential values&lt;BR /&gt;&lt;BR /&gt;I ca also confirm this is no homework assignment!</description>
      <pubDate>Thu, 18 Jan 2024 09:16:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911934#M359560</guid>
      <dc:creator>twenty7</dc:creator>
      <dc:date>2024-01-18T09:16:26Z</dc:date>
    </item>
    <item>
      <title>Re: create new columns based on array values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911959#M359565</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/91983"&gt;@twenty7&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I did, however my understanding is that this solution relies on knowing all of the potential values&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not knowing all the potential values REALLY REALLY REALLY implies you want a long data set, and then SAS can determine what values exist and act accordingly. But you also said:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/91983"&gt;@twenty7&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;SPAN&gt;it is needed in this instance as the data is to be exported and shared with non-technical users who need to be able to easily identify which records have a particular code&lt;/SPAN&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If non-technical users need the wide data set, then get a solution from the long data set, and then "transpose" to wide for these non-technical users.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I see&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;has already done that.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jan 2024 12:15:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-columns-based-on-array-values/m-p/911959#M359565</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-01-18T12:15:49Z</dc:date>
    </item>
  </channel>
</rss>

