<?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 columns in a macro step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-columns-in-a-macro-step/m-p/883477#M349072</link>
    <description>&lt;P&gt;This is better suited to an array rather than a macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*fake data;

data random;
    array _c(100) c1-c100;

    do nrows=1 to 50;

        do i=1 to dim(_c);
            _c(i)=rand('bernoulli', 0.8);
        end;
        output;
    end;
run;

data want;
    set random;
    array _c(10) c1-c10;
    array _c_1(101:200) c101-c200;
    array _c_2(201:300) c201 - c300;

    do i=1 to dim(_c);
        _c_1(i+100)=_c(i);
        _c_2(i+200)=_c(i);
    end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really do want a macro, you need to create the macro variable J using %LET and use %Eval for the calculation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data b;
    set random;
    rename c1-c100 = col1-col100;
run;

%macro test;
    %do i=1 %to 100;
        %let j=%eval(&amp;amp;i+100);

        data b;
            set b;
            col&amp;amp;j=col&amp;amp;i;
        run;

    %end;
%mend;

%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 04 Jul 2023 19:32:06 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2023-07-04T19:32:06Z</dc:date>
    <item>
      <title>Create columns in a macro step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-columns-in-a-macro-step/m-p/883474#M349070</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to do the following task;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;we have a dataset of 100 columns&amp;nbsp; named col1 - col 100, all of them with different values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in a new dataset, I want 300 columns : first 100 columns are from the previous dataset, then I add 100 columns that are named col101-col200 in such a way that col125 (for example) will be equal to col25. Same thing for the 100 other columns with col225 being equal to col25 of the original dataset.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought that a macro will be helpful so I created the following one :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro test;
%do i=1 %to 100;
&amp;amp;j=i+100;
data b;set a;
col&amp;amp;j=col&amp;amp;i;
run;
%end;
%mend;
%test;

%macro test;
%do i=1 %to 100;
&amp;amp;j=i+200;
data b;set b;
col&amp;amp;j=col&amp;amp;i;
run;
%end;
%mend;
%test:&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Unfortunately, the code above doesn't work at all, but I would like to know if someone has a suggestion of something that could lead me to my goal.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jul 2023 19:07:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-columns-in-a-macro-step/m-p/883474#M349070</guid>
      <dc:creator>mtrg92</dc:creator>
      <dc:date>2023-07-04T19:07:07Z</dc:date>
    </item>
    <item>
      <title>Re: Create columns in a macro step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-columns-in-a-macro-step/m-p/883476#M349071</link>
      <description>&lt;P&gt;Turn on MPRINT option so you can see the code that is generated.&lt;/P&gt;
&lt;P&gt;The first one is are doing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;amp;j=i+100;
data b;set a;
col101=col1;
run;
data b;set a;
col102=col2;
run;
...
data b;set a;
col201=col101;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the end result is just the last step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your second one is better. Since now you are reading and writing B.&amp;nbsp; So they will have an effect.&amp;nbsp; If the source of the data is actually A then add a step before the macro call to copy A to B.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data b ; set a; run;
%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But why not just move the %DO loop so you do it all in one data step?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;
data b;set a;
%do i=1 %to 100;
&amp;amp;j=i+100;
col&amp;amp;j=col&amp;amp;i;
%end;
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or skip the macro and just use normal old SAS code instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data b;
  set a;
  array in col1-col100;
  array out col101-col200;
  do over in;
      out=in;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jul 2023 19:26:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-columns-in-a-macro-step/m-p/883476#M349071</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-04T19:26:26Z</dc:date>
    </item>
    <item>
      <title>Re: Create columns in a macro step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-columns-in-a-macro-step/m-p/883477#M349072</link>
      <description>&lt;P&gt;This is better suited to an array rather than a macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*fake data;

data random;
    array _c(100) c1-c100;

    do nrows=1 to 50;

        do i=1 to dim(_c);
            _c(i)=rand('bernoulli', 0.8);
        end;
        output;
    end;
run;

data want;
    set random;
    array _c(10) c1-c10;
    array _c_1(101:200) c101-c200;
    array _c_2(201:300) c201 - c300;

    do i=1 to dim(_c);
        _c_1(i+100)=_c(i);
        _c_2(i+200)=_c(i);
    end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really do want a macro, you need to create the macro variable J using %LET and use %Eval for the calculation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data b;
    set random;
    rename c1-c100 = col1-col100;
run;

%macro test;
    %do i=1 %to 100;
        %let j=%eval(&amp;amp;i+100);

        data b;
            set b;
            col&amp;amp;j=col&amp;amp;i;
        run;

    %end;
%mend;

%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jul 2023 19:32:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-columns-in-a-macro-step/m-p/883477#M349072</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-07-04T19:32:06Z</dc:date>
    </item>
  </channel>
</rss>

