<?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: How to split large dataset into multiple text files in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702363#M79897</link>
    <description>&lt;P&gt;No macro needed. Multiple text files can be written in one data step, using a FILEVAR= option in the FILE statement.&lt;/P&gt;
&lt;P&gt;Sort by month_year first, and use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by month_year;
if first.month_year then /* set FILEVAR= variable here */;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 29 Nov 2020 23:05:40 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-11-29T23:05:40Z</dc:date>
    <item>
      <title>How to split large dataset into multiple text files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702360#M79896</link>
      <description>&lt;P&gt;Hi All!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just need some help in creating a macro for a large data set I have. The dataset has two columns 1)Month_Year (e.g 201511) 2) IP address . For example&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Month_Year &amp;nbsp; &amp;nbsp; &amp;nbsp;IP&lt;/P&gt;&lt;P&gt;201307 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; xxx.1233.xxxx.&lt;/P&gt;&lt;P&gt;201804 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 364.xxx.277&lt;/P&gt;&lt;P&gt;201702 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 947.3734.xxxx&lt;/P&gt;&lt;P&gt;201804 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 73.6282.xxx&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want a macro that creates a file for each month_year containing a list of the IP relating to that month.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So far this is what I've got :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql no print;&lt;/P&gt;&lt;P&gt;select distinct month_year into : month_year&lt;/P&gt;&lt;P&gt;separated by ' '&lt;/P&gt;&lt;P&gt;from mydata&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;order by month_year;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro test;&lt;/P&gt;&lt;P&gt;%do i=1 %to %sysfunc (countw(&amp;amp;month_year));&lt;/P&gt;&lt;P&gt;%let k= %sysfunc (scan(&amp;amp;&amp;amp;month_year,&amp;amp;i));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;data data_want_&amp;amp;k.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;set mydata&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if month_year ="&amp;amp;k.";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc export data =&amp;nbsp;data_want_&amp;amp;k.&lt;/P&gt;&lt;P&gt;outfile = "\\C:\My folder\&amp;amp;k.&lt;/P&gt;&lt;P&gt;dbms= tab replace;&lt;/P&gt;&lt;P&gt;putnames=NO;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%end&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;%test;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I have tried running - I'm not getting any errors, but I'm also not getting any output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help will be much appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Nov 2020 22:57:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702360#M79896</guid>
      <dc:creator>JoseSAS79</dc:creator>
      <dc:date>2020-11-29T22:57:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to split large dataset into multiple text files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702363#M79897</link>
      <description>&lt;P&gt;No macro needed. Multiple text files can be written in one data step, using a FILEVAR= option in the FILE statement.&lt;/P&gt;
&lt;P&gt;Sort by month_year first, and use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by month_year;
if first.month_year then /* set FILEVAR= variable here */;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 29 Nov 2020 23:05:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702363#M79897</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-29T23:05:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to split large dataset into multiple text files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702388#M79898</link>
      <description>&lt;P&gt;Here's a more complete example using&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;'s suggestion, but on the sashelp.citiday dataset.&amp;nbsp; This code uses the "mod" option on the file statement, which means you can write intermittently to any of the raw output files without previous output being overwritten every time an output file is re-opened (which is the default behavior of the file statement).&amp;nbsp; The benefit would be that pre-sorting by year/month would not be needed.&amp;nbsp; There is one file for each year/month combination extracted from the variable DATE:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set sashelp.citiday;
  fvar=cats('c:\temp\',year(date),'_',put(month(date),z2.),'.csv');
  file txt disk filevar=fvar mod dlm=',';
  put date snydjcm;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course, using MOD means you probably want to delete the output files (c:\temp\1988_01.csv, c:\temp\1988_02.csv, ... for the above example) prior to running the data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Nov 2020 04:26:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702388#M79898</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-11-30T04:26:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to split large dataset into multiple text files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702445#M79900</link>
      <description>&lt;P&gt;Thanks so much for the reply. I've never really used filevar, so I've put it together below after reviewing a few posts in the forum. The final text files should only have a list of IP addresses. Please let me know if I've got the idea:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;PROC SORT DATA= Mydata OUT=Mydata_sorted;&lt;BR /&gt;BY month_year;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA _NULL_;&lt;/P&gt;&lt;P&gt;SET mydata_sorted;&amp;nbsp;&lt;BR /&gt;BY month_year;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;if first.make then out_file=cats(‘/folders/myfolders/’, trim(month_year));&lt;/P&gt;&lt;P&gt;file temp filevar=out_file dlm=’,’ dsd;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;if first.make then&lt;BR /&gt;put ‘IP';&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;put IP;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The example I saw had two put statements as well - but don't really understand why.&amp;nbsp;&lt;BR /&gt;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Mon, 30 Nov 2020 10:02:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702445#M79900</guid>
      <dc:creator>JoseSAS79</dc:creator>
      <dc:date>2020-11-30T10:02:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to split large dataset into multiple text files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702458#M79901</link>
      <description>&lt;P&gt;The FIRST. variables are created for the variables in the BY statement, so your code should be this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set mydata_sorted; 
by month_year; 
if first.month_year then out_file = cats(‘/folders/myfolders/’, month_year);
/* no TRIM needed, CATS does that on its own */
file temp filevar=out_file dlm=’,’ dsd; /* do you need a delimiter with only one column? */
if first.month_year then put ‘IP'; /* creates a header in each file */
put IP;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 Nov 2020 10:19:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702458#M79901</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-30T10:19:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to split large dataset into multiple text files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702598#M79903</link>
      <description>&lt;P&gt;I wouldn't make the filename conditionally.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;out_file = cats('/folders/myfolders/', month_year,'.txt');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do then you need to either RETAIN the variable&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain out_file;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or only execute the FILE statement once per group.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.month_year then do;
  out_file = cats('/folders/myfolders/', month_year,'.txt');
  file out filevar=out_file dlm=’,’ dsd; 
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS Some Microsoft "stupid" quotes got mixed into your posted code.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Nov 2020 18:26:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702598#M79903</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-11-30T18:26:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to split large dataset into multiple text files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702600#M79904</link>
      <description>&lt;P&gt;Good catch. Better to set the filevar unconditionally. And those funny quotes come from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/358970"&gt;@JoseSAS79&lt;/a&gt;'s post, as I copy/pasted the code directly from there.&lt;/P&gt;
&lt;P&gt;To &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/358970"&gt;@JoseSAS79&lt;/a&gt;&amp;nbsp;never move code through any kind of word processor.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Nov 2020 18:33:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-split-large-dataset-into-multiple-text-files/m-p/702600#M79904</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-30T18:33:43Z</dc:date>
    </item>
  </channel>
</rss>

