<?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 need help exporting each line of the dataset into a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745297#M233591</link>
    <description>&lt;P&gt;Dear SAS community&lt;/P&gt;
&lt;P&gt;I have a csv dataset (a flat sheet) that contains 100 fields of data from 75 patients.&amp;nbsp; Each patient only has 1 line of data and each patient is identified by the variable "pt_id".&amp;nbsp; I need to write a code to export each line from each patient into a separate csv file, thus generating 75 csv files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do you have an example of such a code?&lt;/P&gt;
&lt;P&gt;thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 02 Jun 2021 19:31:37 GMT</pubDate>
    <dc:creator>rykwong</dc:creator>
    <dc:date>2021-06-02T19:31:37Z</dc:date>
    <item>
      <title>need help exporting each line of the dataset into a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745297#M233591</link>
      <description>&lt;P&gt;Dear SAS community&lt;/P&gt;
&lt;P&gt;I have a csv dataset (a flat sheet) that contains 100 fields of data from 75 patients.&amp;nbsp; Each patient only has 1 line of data and each patient is identified by the variable "pt_id".&amp;nbsp; I need to write a code to export each line from each patient into a separate csv file, thus generating 75 csv files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do you have an example of such a code?&lt;/P&gt;
&lt;P&gt;thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jun 2021 19:31:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745297#M233591</guid>
      <dc:creator>rykwong</dc:creator>
      <dc:date>2021-06-02T19:31:37Z</dc:date>
    </item>
    <item>
      <title>Re: need help exporting each line of the dataset into a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745299#M233592</link>
      <description>Do you want the filenames to be anything specific?&lt;BR /&gt;This is easily possible within a data step. &lt;BR /&gt;</description>
      <pubDate>Wed, 02 Jun 2021 19:34:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745299#M233592</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-02T19:34:31Z</dc:date>
    </item>
    <item>
      <title>Re: need help exporting each line of the dataset into a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745302#M233593</link>
      <description>&lt;P&gt;Here's an example of splitting by group, in your case PT_ID would be your group. &lt;BR /&gt;&lt;A href="https://gist.github.com/statgeek/047bc83a85672f4dd546" target="_blank" rel="noopener"&gt;https://gist.github.com/statgeek/047bc83a85672f4dd546&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRIKE&gt;The only issue with this code is it explicitly lists the variable names and you probably want to do that in a more automated fashion.....&lt;/STRIKE&gt;&lt;BR /&gt;&lt;BR /&gt;EDIT: I updated my example in the link to be more generic, the file names will be the PT_ID as well in this example. HTH.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jun 2021 20:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745302#M233593</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-02T20:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: need help exporting each line of the dataset into a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745374#M233625</link>
      <description>&lt;P&gt;this works!&amp;nbsp; thanks so much !!&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 01:38:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745374#M233625</guid>
      <dc:creator>rykwong</dc:creator>
      <dc:date>2021-06-03T01:38:18Z</dc:date>
    </item>
    <item>
      <title>Re: need help exporting each line of the dataset into a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745448#M233654</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16220"&gt;@rykwong&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Nice that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;solution solved your issue. Please that as a solution.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 12:30:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745448#M233654</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-06-03T12:30:01Z</dc:date>
    </item>
    <item>
      <title>Re: need help exporting each line of the dataset into a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745890#M233915</link>
      <description>&lt;P&gt;Hi Reeza&lt;/P&gt;
&lt;P&gt;Much appreciated.&amp;nbsp; what if the var_split is really from combining 2 variables.&amp;nbsp; To create the unique pt_id I combined “subject_id” with “visit” by pt_id=subject_id||visit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code generated the single line dataset just fine, but it added the pt_id to the end of the dataset.&amp;nbsp; I need to get rid of this variable “pt_id” from the exported datasheets.&amp;nbsp; Alternatively can the code you provided me, handled 2 variables as the var_split?&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jun 2021 19:33:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745890#M233915</guid>
      <dc:creator>rykwong</dc:creator>
      <dc:date>2021-06-04T19:33:41Z</dc:date>
    </item>
    <item>
      <title>Re: need help exporting each line of the dataset into a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745909#M233920</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*name of the data set with the original data;
%let lib_name = sashelp;
%let dsn_name = class;
*Variable to split on;
%let var_split1 = NAME;
%let var_split2 = SEX;
*path to folder to save text files;
%let path_folder= /home/fkhurshed/;




*if you are exporting each line this is not required 
but should not  cause any issues unless your data set is large. In that case rename your data set to _temp and skip this step;
PROC SORT DATA=&amp;amp;lib_name..&amp;amp;dsn_name OUT=_temp;
BY &amp;amp;var_split1 &amp;amp;var_split2;
RUN;



*make variable lists;
*for header row;
proc sql noprint;
select name into :var_list_csv separated by ", " from sashelp.vcolumn where libname = upper("&amp;amp;lib_name") and memname = upper("&amp;amp;dsn_name");
select name into :var_list separated by " " from sashelp.vcolumn where libname = upper("&amp;amp;lib_name") and memname = upper("&amp;amp;dsn_name");
quit;



DATA _NULL_;

SET _temp; *Dataset to be exported;
BY &amp;amp;var_split1 &amp;amp;var_split2.; *Variable that file is to be split on;

*Create path to file that is to be exported;
if first.&amp;amp;var_split2. then out_file=cats("&amp;amp;path_folder.", &amp;amp;var_split., ".csv");

file temp filevar=out_file dlm=',' dsd;

*If first value of make then output column names;
if first.&amp;amp;var_split2. then 
put "&amp;amp;var_list_csv.";

*Output variables;
put &amp;amp;var_list.;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, but this should work for two variables.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jun 2021 20:23:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/745909#M233920</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-04T20:23:25Z</dc:date>
    </item>
    <item>
      <title>Re: need help exporting each line of the dataset into a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/747155#M234455</link>
      <description>&lt;P&gt;Dear Reeza&lt;/P&gt;
&lt;P&gt;I ran it but encounter this error (please see attached log)&lt;/P&gt;
&lt;P&gt;Many thanks for your help.&amp;nbsp; Please let me know your thoughts&lt;/P&gt;
&lt;P&gt;Thanks again&lt;/P&gt;</description>
      <pubDate>Thu, 10 Jun 2021 17:20:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/747155#M234455</guid>
      <dc:creator>rykwong</dc:creator>
      <dc:date>2021-06-10T17:20:26Z</dc:date>
    </item>
    <item>
      <title>Re: need help exporting each line of the dataset into a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/747185#M234477</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*name of the data set with the original data;
%let lib_name = sashelp;
%let dsn_name = class;
*Variable to split on;
%let var_split1 = NAME;
%let var_split2 = SEX;
*path to folder to save text files;
%let path_folder= /home/fkhurshed/;




*if you are exporting each line this is not required 
but should not  cause any issues unless your data set is large. In that case rename your data set to _temp and skip this step;
PROC SORT DATA=&amp;amp;lib_name..&amp;amp;dsn_name OUT=_temp;
BY &amp;amp;var_split1 &amp;amp;var_split2;
RUN;



*make variable lists;
*for header row;
proc sql noprint;
select name into :var_list_csv separated by ", " from sashelp.vcolumn where libname = upper("&amp;amp;lib_name") and memname = upper("&amp;amp;dsn_name");
select name into :var_list separated by " " from sashelp.vcolumn where libname = upper("&amp;amp;lib_name") and memname = upper("&amp;amp;dsn_name");
quit;



DATA _NULL_;

SET _temp; *Dataset to be exported;
BY &amp;amp;var_split1 &amp;amp;var_split2.; *Variable that file is to be split on;

*Create path to file that is to be exported;
if first.&amp;amp;var_split2. then out_file=cats("&amp;amp;path_folder.", &amp;amp;var_split1., "_", &amp;amp;var_split2., ".csv");

file temp filevar=out_file dlm=',' dsd;

*If first value of make then output column names;
if first.&amp;amp;var_split2. then 
put "&amp;amp;var_list_csv.";

*Output variables;
put &amp;amp;var_list.;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've changed the line to as follows - this is the part that controls the file name.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.&amp;amp;var_split2. then out_file=cats("&amp;amp;path_folder.", trim(&amp;amp;var_split1.), "_", trim(&amp;amp;var_split2.), ".csv");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So now it will include the firstSplitVariable_secondSplitVariable in the name. If you'd like only one portion, remove it from the CATS() function.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Jun 2021 19:01:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/747185#M234477</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-10T19:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: need help exporting each line of the dataset into a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/747189#M234481</link>
      <description>&lt;P&gt;Note that the IF FIRST. is not needed when generating the filename. The FILE statement will take care of noticing when the value of the FILEVAR= variable changes and switch files automatically.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SORT is not needed either especially if you are only writing one line per file.&amp;nbsp; But if you are writing multiple lines to the same file then processing the records in filename order it will saving some time as it will prevent SAS from opening and closing the same file multiple times.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Jun 2021 19:16:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/747189#M234481</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-10T19:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: need help exporting each line of the dataset into a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/747241#M234501</link>
      <description>I think I had the same comment about the sort in the code &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Thanks for the tip for the FILEVAR automatically detecting changes, that's a good thing to know!&lt;BR /&gt;&lt;BR /&gt;This is a generic program, so it's intended to handle both a single record or multiple records, so a bit of overkill in places but generic enough to be useful IMO.</description>
      <pubDate>Fri, 11 Jun 2021 00:07:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-exporting-each-line-of-the-dataset-into-a-dataset/m-p/747241#M234501</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-11T00:07:39Z</dc:date>
    </item>
  </channel>
</rss>

