<?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: generate new variable for multiple files using loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/generate-new-variable-for-multiple-files-using-loop/m-p/617082#M180764</link>
    <description>&lt;P&gt;Something like below should do the job.&lt;/P&gt;
&lt;P&gt;To deal with the different lenghts: Use a LENGTH statement prior to the SET statement and there define the max length from any of the source tables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data file1 file2 file3;
  set sashelp.class;
  testvar='1';
run;

data file4;
  set sashelp.class;
  testvar='22';
run;

data want;
  length testvar $2;
  set file1 - file4 indsname=_sourceFile;
  newar=2019-age;
  length sourceFile $32;
  sourceFile=_sourceFile;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 13 Jan 2020 22:21:51 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2020-01-13T22:21:51Z</dc:date>
    <item>
      <title>generate new variable for multiple files using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generate-new-variable-for-multiple-files-using-loop/m-p/617070#M180757</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to generate a new variable in 20 different files. It seems that I can do it at once using a loop function. Would you please let me know how to do it?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a 20 sas data files: file1-file20&lt;/P&gt;&lt;P&gt;I simply want to generate one variable in each file: Age = 2019- Byear&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data file1;
set file1;
age=2019-byear;
run;


* I am looking for a code to run the codes above at once for 20 files.;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, can anyone let me know how to change the format of each variables? I need to append the data (file1-file20) but it has an error due to different lengths... Is there anyway I can append them without losing any obs? Thank you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2020 21:49:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generate-new-variable-for-multiple-files-using-loop/m-p/617070#M180757</guid>
      <dc:creator>cphd</dc:creator>
      <dc:date>2020-01-13T21:49:30Z</dc:date>
    </item>
    <item>
      <title>Re: generate new variable for multiple files using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generate-new-variable-for-multiple-files-using-loop/m-p/617081#M180763</link>
      <description>&lt;P&gt;Do all your files (they are sas data sets, right?)&amp;nbsp;have identical variables?&amp;nbsp; If so, here is a non-macro-ized way to do it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data newfile1 newfile2 ....   newfile20;
  set file1 (in=in1) file2 (in=in2) .... newfile20 (in=in20);
  age=2019-byear;
  if in1  then output newfile1;  else
  if in2  then output newfile2;  else
  ....
  if in20 then output newfile20;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But that's a lot of typing, and requires that all the data sets have identical variables.&amp;nbsp; Here's a way that takes less typing and accommodates different variable collections&amp;nbsp;between the data sets.&amp;nbsp; But it requires defining, and using, a macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro do_all_20;
  %do i=1 %to 20;
  data newfile&amp;amp;i;
    set file&amp;amp;i;
    age=2019-byear;
  run;
%mend;
%do_all_20;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first program runs a single data step, inputting (and outputting) 20 datasets.&amp;nbsp; The second runs 20 data steps, each with one input and output dataset.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2020 22:20:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generate-new-variable-for-multiple-files-using-loop/m-p/617081#M180763</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-01-13T22:20:13Z</dc:date>
    </item>
    <item>
      <title>Re: generate new variable for multiple files using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generate-new-variable-for-multiple-files-using-loop/m-p/617082#M180764</link>
      <description>&lt;P&gt;Something like below should do the job.&lt;/P&gt;
&lt;P&gt;To deal with the different lenghts: Use a LENGTH statement prior to the SET statement and there define the max length from any of the source tables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data file1 file2 file3;
  set sashelp.class;
  testvar='1';
run;

data file4;
  set sashelp.class;
  testvar='22';
run;

data want;
  length testvar $2;
  set file1 - file4 indsname=_sourceFile;
  newar=2019-age;
  length sourceFile $32;
  sourceFile=_sourceFile;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Jan 2020 22:21:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generate-new-variable-for-multiple-files-using-loop/m-p/617082#M180764</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-01-13T22:21:51Z</dc:date>
    </item>
    <item>
      <title>Re: generate new variable for multiple files using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generate-new-variable-for-multiple-files-using-loop/m-p/617086#M180766</link>
      <description>&lt;P&gt;Is there a reason to keep these 20 files separate? Can you combine them and then process everything together, which seems like the most efficient solution. If not you could look into a macro (see tutorials below) or examples of loops in the second link.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This would run the code for all file data sets but the output would all be stored in a single output data set, called file_combined.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data file_combined;
set file1-file20 indsname=source;
&lt;BR /&gt;dsn_source = source;
age = 2019-byear;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Tutorial on converting a working program to a macro&lt;BR /&gt;&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Examples of common macro usage&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&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/306356"&gt;@cphd&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to generate a new variable in 20 different files. It seems that I can do it at once using a loop function. Would you please let me know how to do it?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a 20 sas data files: file1-file20&lt;/P&gt;
&lt;P&gt;I simply want to generate one variable in each file: Age = 2019- Byear&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data file1;
set file1;
age=2019-byear;
run;


* I am looking for a code to run the codes above at once for 20 files.;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, can anyone let me know how to change the format of each variables? I need to append the data (file1-file20) but it has an error due to different lengths... Is there anyway I can append them without losing any obs? Thank you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2020 22:51:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generate-new-variable-for-multiple-files-using-loop/m-p/617086#M180766</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-01-13T22:51:32Z</dc:date>
    </item>
    <item>
      <title>Re: generate new variable for multiple files using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generate-new-variable-for-multiple-files-using-loop/m-p/617230#M180822</link>
      <description>&lt;P&gt;Thanks for sharing the code. I used the macro function to do it but you forgot to add %end at the end. Here is the code that I used for the reference for future users.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro all20;
  %do i=1 %to 20;
  data newfile&amp;amp;i;
    set file&amp;amp;i;
    age=2019-byear;
  run;
%end
%mend all20;
%all20;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 15:59:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generate-new-variable-for-multiple-files-using-loop/m-p/617230#M180822</guid>
      <dc:creator>cphd</dc:creator>
      <dc:date>2020-01-14T15:59:13Z</dc:date>
    </item>
  </channel>
</rss>

