<?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 multiple variables in a dataset based on the name and number of columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-variables-in-a-dataset-based-on-the-name-and/m-p/924302#M363820</link>
    <description>&lt;P&gt;Below should work as long as your source strings don't contain more than 4 characters.&lt;/P&gt;
&lt;P&gt;A more generic solution would require that you first analyse your source strings and then create as many target variables as you need for the longest source string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input a_1 $ a_2 $ a_3 $ a_4 $;
  datalines;
1000 1111 1010 1110
0101 1100 1001 1111
1100 0100 0001 1001
;
run;

data want;
  set have;
  array src_vars{4} a_1 a_2 a_3 a_4;
  array trg_vars{4,4} $1 a_11-a_14 a_21-a_24 a_31-a_34 a_41-a_44;
  do i=1 to dim(src_vars);
    do k=1 to length(src_vars[i]);
      trg_vars[i,k]=substr(src_vars[i],k,1);
    end;
  end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 15 Apr 2024 03:11:39 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2024-04-15T03:11:39Z</dc:date>
    <item>
      <title>Create multiple variables in a dataset based on the name and number of columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-variables-in-a-dataset-based-on-the-name-and/m-p/924300#M363819</link>
      <description>&lt;P&gt;I am trying to create 4 variables for each column in the dataset below. For example, a_1 should be used to create a_1_1, a_1_2, a_1_3 and a_1_4 with each variable containing the 4 individual digits in parent variable a_1 :&amp;nbsp; a_1_1 = 1; a_1_2=0;&amp;nbsp;a_1_3=0;&amp;nbsp;a_1_4=0;&amp;nbsp;&amp;nbsp;So, a total of 16 new variables created.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to accomplish this using arrays and loops in a data step, making it easier to generalize this task?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data check;
input a_1 $ a_2 $ a_3 $ a_4 $;
datalines;
1000 1111 1010 1110
0101 1100 1001 1111
1100 0100 0001 1001
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2024 02:32:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-variables-in-a-dataset-based-on-the-name-and/m-p/924300#M363819</guid>
      <dc:creator>kc</dc:creator>
      <dc:date>2024-04-15T02:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple variables in a dataset based on the name and number of columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-variables-in-a-dataset-based-on-the-name-and/m-p/924302#M363820</link>
      <description>&lt;P&gt;Below should work as long as your source strings don't contain more than 4 characters.&lt;/P&gt;
&lt;P&gt;A more generic solution would require that you first analyse your source strings and then create as many target variables as you need for the longest source string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input a_1 $ a_2 $ a_3 $ a_4 $;
  datalines;
1000 1111 1010 1110
0101 1100 1001 1111
1100 0100 0001 1001
;
run;

data want;
  set have;
  array src_vars{4} a_1 a_2 a_3 a_4;
  array trg_vars{4,4} $1 a_11-a_14 a_21-a_24 a_31-a_34 a_41-a_44;
  do i=1 to dim(src_vars);
    do k=1 to length(src_vars[i]);
      trg_vars[i,k]=substr(src_vars[i],k,1);
    end;
  end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2024 03:11:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-variables-in-a-dataset-based-on-the-name-and/m-p/924302#M363820</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-04-15T03:11:39Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple variables in a dataset based on the name and number of columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-variables-in-a-dataset-based-on-the-name-and/m-p/924303#M363821</link>
      <description>&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to automatically create variable names in the target array below instead of having to spell it out because, I need to generalize the variable creation as the dataset can have any number of source variables. This data step will be part of a macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;array trg_vars{4,4} $1 a_11-a_14 a_21-a_24 a_31-a_34 a_41-a_44;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2024 03:25:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-variables-in-a-dataset-based-on-the-name-and/m-p/924303#M363821</guid>
      <dc:creator>kc</dc:creator>
      <dc:date>2024-04-15T03:25:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple variables in a dataset based on the name and number of columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-variables-in-a-dataset-based-on-the-name-and/m-p/924306#M363822</link>
      <description>&lt;P&gt;Seems pretty simple.&amp;nbsp; Convert it to a TALL structure and then use PROC TRANPOSE.&lt;/P&gt;
&lt;P&gt;You will need a variable to identify the original observations.&amp;nbsp; If you don't have one then just add one when making the TALL dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input a_1 $ a_2 $ a_3 $ a_4 $;
datalines;
1000 1111 1010 1110
0101 1100 1001 1111
1100 0100 0001 1001
;

data tall ;
  row+1;
  set have;
  array x a_1 - a_4 ;
  length _name_ $32 col 8 char $1 ;
  do index=1 to dim(x);
    _name_= vname(x[index]);
    do col=1 to length(x[index]);
      char=char(x[index],col);
      output;
    end;
  end;
run;

proc transpose data=tall out=want(drop=_name_) delim=_;
  by row ;
  id _name_ col;
  var char ;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1713152045962.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95540i7790A98CBF96EDD8/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1713152045962.png" alt="Tom_0-1713152045962.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2024 03:34:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-variables-in-a-dataset-based-on-the-name-and/m-p/924306#M363822</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-04-15T03:34:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple variables in a dataset based on the name and number of columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-variables-in-a-dataset-based-on-the-name-and/m-p/924313#M363826</link>
      <description>&lt;P&gt;A more generalize way is using double PROC TRANSPOSE just like Tom's code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input a_1 $ a_2 $ a_3 $ a_4 $;
  datalines;
1000 1111 1010 1110
0101 1100 1001 1111
1100 0100 0001 1001
;
run;
data temp;
 set have;
 id+1;
run;
proc transpose data=temp out=temp2;
by id;
var a:;
run;
data temp3;
 set temp2;
 do i=1 to lengthn(col1);
  v=char(col1,i);output;
 end;
run;
proc transpose data=temp3 out=want(drop=_name_) delimiter=_;
by id ;
id _name_ i;
var v;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Apr 2024 06:19:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-variables-in-a-dataset-based-on-the-name-and/m-p/924313#M363826</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-04-15T06:19:58Z</dc:date>
    </item>
  </channel>
</rss>

