<?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 Export each observation from a dataset using macros do loops in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799065#M314166</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.ds;
set sashelp.class;
run;

proc sql;
select count(Name) into :N from work.ds; 
select name,age,sex into : names1- from work.ds;
quit;

%macro export_data;
%do i=1 %to &amp;amp;N. ;
proc export data=ds(where=(name="&amp;amp;&amp;amp;names&amp;amp;i."))
            outfile="/home/u35263349/My_Folder/&amp;amp;&amp;amp;names&amp;amp;i..xlsx"
            dbms=xlsx  replace;
            run;
            %end;
%mend;
%export_data;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to export each observation into each excel file&amp;nbsp; but no data shown after export&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FYI&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Anandkvn_0-1646021746631.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68997i43FB0FD08FF485BE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Anandkvn_0-1646021746631.png" alt="Anandkvn_0-1646021746631.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 28 Feb 2022 04:16:00 GMT</pubDate>
    <dc:creator>BrahmanandaRao</dc:creator>
    <dc:date>2022-02-28T04:16:00Z</dc:date>
    <item>
      <title>Export each observation from a dataset using macros do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799065#M314166</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.ds;
set sashelp.class;
run;

proc sql;
select count(Name) into :N from work.ds; 
select name,age,sex into : names1- from work.ds;
quit;

%macro export_data;
%do i=1 %to &amp;amp;N. ;
proc export data=ds(where=(name="&amp;amp;&amp;amp;names&amp;amp;i."))
            outfile="/home/u35263349/My_Folder/&amp;amp;&amp;amp;names&amp;amp;i..xlsx"
            dbms=xlsx  replace;
            run;
            %end;
%mend;
%export_data;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to export each observation into each excel file&amp;nbsp; but no data shown after export&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FYI&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Anandkvn_0-1646021746631.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68997i43FB0FD08FF485BE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Anandkvn_0-1646021746631.png" alt="Anandkvn_0-1646021746631.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Feb 2022 04:16:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799065#M314166</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2022-02-28T04:16:00Z</dc:date>
    </item>
    <item>
      <title>Re: Export each observation from a dataset using macros do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799070#M314169</link>
      <description>&lt;P&gt;Try to increase "." after "&amp;amp;i.".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc export data=ds(where=(name="&amp;amp;&amp;amp;names&amp;amp;i.."))
            outfile="/home/u35263349/My_Folder/&amp;amp;&amp;amp;names&amp;amp;i...xlsx"
            dbms=xlsx  replace;
            run;
            %end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Feb 2022 04:26:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799070#M314169</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2022-02-28T04:26:49Z</dc:date>
    </item>
    <item>
      <title>Re: Export each observation from a dataset using macros do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799072#M314171</link>
      <description>&lt;P&gt;Check the version of SAS you are running.&amp;nbsp; XLSX engine is "recent".&lt;/P&gt;
&lt;PRE&gt;1744  %put &amp;amp;=sysvlong;
SYSVLONG=9.04.01M5P091317
&lt;/PRE&gt;
&lt;P&gt;I was able to run this code on SAS 9.4M5 on Windows.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc export
  data=sashelp.class(where=(name="Alfred"))
  outfile="c:\downloads\Alfred.xlsx"
  dbms=xlsx  replace
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And it makes an appropriate file.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2022-02-27 232419.jpg" style="width: 527px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68998i5592B3FA556986AD/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2022-02-27 232419.jpg" alt="Screenshot 2022-02-27 232419.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code making the macro variables is a little confused.&amp;nbsp; There is no need to count in advance of making the macro variables.&amp;nbsp; SQL will count for you.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select name 
  into :names1- 
  from work.ds
;
%let n=&amp;amp;sqlobs;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If your variable has leading spaces then that method of making the macro variables will remove them which might explain your issue with zero observations.&amp;nbsp; You might need to add quotes into the macro variables.&amp;nbsp; But watch out if you are also using those macro variables to generate the filename.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Feb 2022 04:32:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799072#M314171</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-28T04:32:53Z</dc:date>
    </item>
    <item>
      <title>Re: Export each observation from a dataset using macros do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799076#M314175</link>
      <description>&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;This is my version in SAS ondemands&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;%put &amp;amp;=sysvlong;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;SYSVLONG=9.04.01M6P110718&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;This is not give any output in log window i how many observations in dataset by using sqlobs statement&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;proc sql noprint;
select name 
  into :names1- 
  from work.ds
;
%let n=&amp;amp;sqlobs;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;Finally i did not&amp;nbsp; get output same its show empty excel sheet&lt;/DIV&gt;</description>
      <pubDate>Mon, 28 Feb 2022 04:54:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799076#M314175</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2022-02-28T04:54:28Z</dc:date>
    </item>
    <item>
      <title>Re: Export each observation from a dataset using macros do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799078#M314177</link>
      <description>&lt;P&gt;The real problem with your code as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/226565"&gt;@japelin&lt;/a&gt;&amp;nbsp;pointed out is that you are making filenames without the .xlsx extension because you did not include enough periods.&amp;nbsp;&amp;nbsp;But that shouldn't cause it to not write the actual data.&amp;nbsp; Perhaps you are just looking at some old file instead of the ones your program generated?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using the NOPRINT option will prevent PROC SQL from cluttering the output window with the list of names you are just trying to get into macro variables. If you want to see the values of the macro variables in the SAS log then use %PUT statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put &amp;amp;=sqlobs &amp;amp;=n &amp;amp;=name1;&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, 28 Feb 2022 05:04:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799078#M314177</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-28T05:04:39Z</dc:date>
    </item>
    <item>
      <title>Re: Export each observation from a dataset using macros do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799079#M314178</link>
      <description>&lt;P&gt;Make sure you haven't somehow gotten the system option OBS set to zero.&amp;nbsp; SAS will sometimes do that in response to some error in an earlier step in your code.&lt;/P&gt;
&lt;PRE&gt;1771  options obs=0;
1772  proc export
1773    data=sashelp.class(where=(name="Alfred"))
1774    outfile="c:\downloads\alfred.xlsx"
1775    dbms=xlsx  replace
1776  ;
1777  run;

&lt;FONT color="#FF0000"&gt;NOTE: The export data set has 0 observations and 5 variables.&lt;/FONT&gt;
NOTE: "c:\downloads\alfred.xlsx" file was successfully created.
NOTE: PROCEDURE EXPORT used (Total process time):
      real time           0.09 seconds
      cpu time            0.03 seconds
&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Feb 2022 05:08:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799079#M314178</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-28T05:08:47Z</dc:date>
    </item>
    <item>
      <title>Re: Export each observation from a dataset using macros do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799080#M314179</link>
      <description>&lt;P&gt;Thank you Tom&lt;/P&gt;
&lt;P&gt;understood your point&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _null_;&lt;BR /&gt;set work.ds;&lt;BR /&gt;call execute('data ' !! compress(Name) !! '; &lt;BR /&gt;set work.ds; &lt;BR /&gt;where Name = "' !! Name !! '"; run;');&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;CAN WE EXPORT ABOVE DATASET USING PROC EXPORT HOW EXOPORT _NULL_ DATASET&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Feb 2022 05:32:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799080#M314179</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2022-02-28T05:32:12Z</dc:date>
    </item>
    <item>
      <title>Re: Export each observation from a dataset using macros do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799081#M314180</link>
      <description>&lt;P&gt;Thank you Japelin&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Feb 2022 05:47:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799081#M314180</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2022-02-28T05:47:15Z</dc:date>
    </item>
    <item>
      <title>Re: Export each observation from a dataset using macros do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799095#M314187</link>
      <description>&lt;P&gt;This code works for me (on SAS ODA) and creates non-empty spreadsheets:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.ds;
set sashelp.class;
run;

proc sql noprint;
select count(Name) into :N from work.ds; 
select name into : names1- from work.ds;
quit;

%macro export_data;
%do i=1 %to &amp;amp;N. ;
  proc export
    data=ds (where=(name="&amp;amp;&amp;amp;names&amp;amp;i.."))
    outfile="~/Anandkvn/&amp;amp;&amp;amp;names&amp;amp;i...xlsx"
    dbms=xlsx
    replace
  ;
  run;
%end;
%mend;
%export_data;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Feb 2022 08:52:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799095#M314187</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-28T08:52:01Z</dc:date>
    </item>
    <item>
      <title>Re: Export each observation from a dataset using macros do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799158#M314214</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265860"&gt;@BrahmanandaRao&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;This code works for me too and creates one spreadsheet for every name and having one row of data.&lt;BR /&gt;I believe your code is OK.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Feb 2022 16:08:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799158#M314214</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2022-02-28T16:08:59Z</dc:date>
    </item>
    <item>
      <title>Re: Export each observation from a dataset using macros do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799219#M314232</link>
      <description>&lt;P&gt;The data _NULL_ step does not create any output dataset.&amp;nbsp; It is just using CALL EXECUTE() to stack up some SAS code to run after the data _NULL_ step finishes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Feb 2022 22:38:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-each-observation-from-a-dataset-using-macros-do-loops/m-p/799219#M314232</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-28T22:38:52Z</dc:date>
    </item>
  </channel>
</rss>

