<?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: Conditionally Exporting SAS to Excel in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684493#M79538</link>
    <description>&lt;P&gt;I fixed the code but could not test it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let today = SEP20;
data _NULL_;
 set Work.Cases;
  by state notsorted;
     if first.state then
        call execute ("proc export data=" || strip(state) || " " ||
        cats('outfile="C:\Users\JPark\Documents\Test\',strip(state),"_&amp;amp;today..xlsx",'" dbms=xlsx replace; run;'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 17 Sep 2020 04:16:16 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2020-09-17T04:16:16Z</dc:date>
    <item>
      <title>Conditionally Exporting SAS to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684366#M79521</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Warning: I am a SAS Novice so advanced help will not help me, if that makes sense. I have spent hours googling trying to figure this out and would like to ask for help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Goal:&lt;/P&gt;&lt;P&gt;I am trying to parse out a spreadsheet with data from many US states into separate spreadsheets, with a separate spreadsheet for each state.&lt;/P&gt;&lt;P&gt;My data source spreadsheet has variables are Location, Cost, etc, etc. &lt;STRONG&gt;State.&amp;nbsp;&lt;/STRONG&gt;State is what I am dividing out by.&lt;/P&gt;&lt;P&gt;I am not yet advanced enough to learn how to do macros/SQL, so I am okay with having 50 export statements (1 for each state).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My goal here is to make it so SAS does&amp;nbsp;&lt;EM&gt;not&lt;/EM&gt; export/create a spreadsheet if a state does not appear in Testdata.xlsx.&lt;/P&gt;&lt;P&gt;Right now my code creates a spreadsheet for states even if they do not appear under the variable name/column header 'State' in Testdata.xlsx.&lt;/P&gt;&lt;P&gt;So if my spreadsheet has 10 rows/obs of data, each one for a different US state, I end up creating 50 total exports, of which 40 are useless.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I have so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC IMPORT OUT = Work.Separate&lt;BR /&gt;datafile="\\Test\Testdata.xlsx"&lt;BR /&gt;dbms=xlsx replace;&lt;BR /&gt;sheet="Sheet1";&lt;BR /&gt;getnames=yes;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let today=&amp;amp;sysdate;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data AL;&lt;BR /&gt;set Work.Separate;&lt;BR /&gt;if State='AL';&lt;BR /&gt;proc export data=AL&lt;BR /&gt;outfile="\\Test\AL &amp;amp;today..xlsx"&lt;BR /&gt;dbms=xlsx replace;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data AZ;&lt;BR /&gt;set Work.Separate;&lt;BR /&gt;if State='AK';&lt;BR /&gt;proc export data=AK&lt;BR /&gt;outfile="\\Test\AK &amp;amp;today..xlsx"&lt;BR /&gt;dbms=xlsx replace;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank You!!&lt;/P&gt;&lt;P&gt;Joseph&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 20:37:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684366#M79521</guid>
      <dc:creator>LearningSAS2</dc:creator>
      <dc:date>2020-09-16T20:37:27Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Exporting SAS to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684368#M79523</link>
      <description>&lt;P&gt;Could we get some clarification?&amp;nbsp; You say you are creating spreadsheets, but this is creating SAS datasets.&amp;nbsp; If you are generating spreadsheets using ODS, most output procedures (proc report, print, others) have a "BY" statement that will create separate tables by value for you.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 20:47:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684368#M79523</guid>
      <dc:creator>CurtisMackWSIPP</dc:creator>
      <dc:date>2020-09-16T20:47:25Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Exporting SAS to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684369#M79524</link>
      <description>Hello, I think it looks like I am creating datasets because I shortened my file paths for outfile and datafile (it is very long and took up a lot of space).&lt;BR /&gt;&lt;BR /&gt;Maybe I am misunderstanding what you mean? Sorry and thank you!</description>
      <pubDate>Wed, 16 Sep 2020 20:50:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684369#M79524</guid>
      <dc:creator>LearningSAS2</dc:creator>
      <dc:date>2020-09-16T20:50:56Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Exporting SAS to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684372#M79525</link>
      <description>&lt;P&gt;I'm saying that you can handle this in the output step with a "BY" variable.&amp;nbsp; Here is an example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sort  data= sashelp.baseball out= sorted;
  by team;
run;

ods excel file="c:\temp\temp.xlsx";

proc print data=sorted;
  by team;
run;

ods excel close;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Sep 2020 21:01:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684372#M79525</guid>
      <dc:creator>CurtisMackWSIPP</dc:creator>
      <dc:date>2020-09-16T21:01:14Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Exporting SAS to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684388#M79528</link>
      <description>Thank You!&lt;BR /&gt;I have no idea SAS could do this.&lt;BR /&gt;&lt;BR /&gt;Is there any way ODS can make a separate .xlsx for each state? Right now I have 1 excel file with many tabs.&lt;BR /&gt;&lt;BR /&gt;Thank You!&lt;BR /&gt;Joseph</description>
      <pubDate>Wed, 16 Sep 2020 21:26:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684388#M79528</guid>
      <dc:creator>LearningSAS2</dc:creator>
      <dc:date>2020-09-16T21:26:38Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Exporting SAS to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684391#M79529</link>
      <description>&lt;P&gt;1) In order to eliminate the misunderstanding you should use the running man icon to post the code&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;and better separate the data step from a proc as shown here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data AL;
   set Work.Separate;
         if State='AL';
run;

proc export data=AL
     outfile="\\Test\AL &amp;amp;today..xlsx"
     dbms=xlsx replace;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) In your 2nd state you probably have a typo:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data AK;   /* not AZ; */
   set Work.Separate;
        if State='AK';
run;

proc export data=AK
    outfile="\\Test\AK &amp;amp;today..xlsx"
     dbms=xlsx replace;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I can think of few methods doing what you want - exporting only those states given in the input:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;a)&amp;nbsp; Run PROC FREQ to check what states are in the input and write PROC EXPORT only for them&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=Work.Separate;
     table state;
run;

/* check results then add ... */
data AL AK ;
 set Work.Separate;
      if state = 'AL' then output AL; else
      if state = 'AK' then output AK;  /* ... else more states */
run;

proc export data=AL
   outfile="\\Test\AL &amp;amp;today..xlsx"
   dbms=xlsx replace;
run;

proc export data=AK
   outfile="\\Test\AK &amp;amp;today..xlsx"
   dbms=xlsx replace;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;b) Use CALL EXECUTE from a datstep without the need to cteare a dataset per each state, as in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let today = SEP20;&lt;BR /&gt;data _NULL_;&lt;BR /&gt;   set work.separated;&lt;BR /&gt;    by state notsorted;&lt;BR /&gt;      if first.state then &lt;BR /&gt;         call execute ("proc export data" || strip(state) ||&lt;BR /&gt;              cats('outfile="\\Test\',strip(state),"&amp;amp;today..xlsx",'" dbms=xlsx replace; run;" '));&lt;BR /&gt;&lt;BR /&gt;run;&lt;BR /&gt;    &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;c) Using a macro, but that seems to be more complicated for you now.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 21:50:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684391#M79529</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-09-16T21:50:53Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Exporting SAS to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684393#M79530</link>
      <description>Unfortunately, I do not know of a way of doing that without your approach or macros.</description>
      <pubDate>Wed, 16 Sep 2020 21:42:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684393#M79530</guid>
      <dc:creator>CurtisMackWSIPP</dc:creator>
      <dc:date>2020-09-16T21:42:20Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Exporting SAS to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684399#M79532</link>
      <description>Thank You&lt;BR /&gt;&lt;BR /&gt;I attempted the Call Execute (I have not learned this yet), but I am seeing this error:&lt;BR /&gt;ERROR: ""\\Test\PASEP20.xlsx"" is not a valid name.&lt;BR /&gt;&lt;BR /&gt;I will most likely to with either ODS or your Proc Freq method to check for states, but just wondering what the above Error means and what I am doing wrong.&lt;BR /&gt;&lt;BR /&gt;Thank You both!</description>
      <pubDate>Wed, 16 Sep 2020 22:04:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684399#M79532</guid>
      <dc:creator>LearningSAS2</dc:creator>
      <dc:date>2020-09-16T22:04:37Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Exporting SAS to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684401#M79533</link>
      <description>&lt;P&gt;Its a typo:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;outfile="c:\Test\AK &amp;amp;today..xlsx"&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Sep 2020 22:07:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684401#M79533</guid>
      <dc:creator>CurtisMackWSIPP</dc:creator>
      <dc:date>2020-09-16T22:07:17Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Exporting SAS to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684402#M79534</link>
      <description>That must be a valid path in your system.</description>
      <pubDate>Wed, 16 Sep 2020 22:07:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684402#M79534</guid>
      <dc:creator>CurtisMackWSIPP</dc:creator>
      <dc:date>2020-09-16T22:07:46Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Exporting SAS to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684403#M79535</link>
      <description>&lt;P&gt;dataPAoutfile has green dashed lines under it, before it stays there is an error (is not a valid name)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc export &lt;STRONG&gt;dataPAoutfile&lt;/STRONG&gt;="\\JPark\Documents\Test\&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 22:08:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684403#M79535</guid>
      <dc:creator>LearningSAS2</dc:creator>
      <dc:date>2020-09-16T22:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Exporting SAS to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684410#M79536</link>
      <description>&lt;PRE&gt;ERROR: ""\\Test\PASEP20.xlsx"" is not a valid name.&lt;/PRE&gt;
&lt;P&gt;it is probably a state PA concatenated to the date 'SEP20' as assigned to the macro variable TODAY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are double - double quotes - generated. I have edited the posted code after testing it. Please try the last edited code.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 22:17:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684410#M79536</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-09-16T22:17:30Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Exporting SAS to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684411#M79537</link>
      <description>&lt;P&gt;Sorry, still encountering the not valid name error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking at my log error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE: Line generated by the CALL EXECUTE routine.&lt;BR /&gt;1 + proc export dataPAoutfile="C:\Users\JPark\Documents\Test\AK SEP20xlsxPAAK SEP20.xlsx"&lt;BR /&gt;-------------&lt;BR /&gt;1&lt;BR /&gt;1 !+dbms=xlsx replace; run;""&lt;BR /&gt;ERROR: ""C:\Users\JPark\Documents\Test\AK SEP20xlsxPAAK SEP20.xlsx"" is not a valid name.&lt;BR /&gt;NOTE: PROCEDURE EXPORT used (Total process time):&lt;BR /&gt;real time 0.00 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC IMPORT OUT = Work.Cases&lt;BR /&gt;datafile="C:\Users\JPark\Documents\Test\Testdata.xlsx"&lt;BR /&gt;dbms=xlsx replace;&lt;BR /&gt;sheet="Sheet1";&lt;BR /&gt;getnames=yes;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%let today = SEP20;data _NULL_;&lt;BR /&gt;set Work.Cases;&lt;BR /&gt;by state notsorted;&lt;BR /&gt;if first.state then&lt;BR /&gt;call execute ("proc export data" || strip(state) ||&lt;BR /&gt;cats('outfile="C:\Users\JPark\Documents\Test\',strip(state),"AK &amp;amp;today..xlsx",'" dbms=xlsx replace; run;" '));&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 22:18:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684411#M79537</guid>
      <dc:creator>LearningSAS2</dc:creator>
      <dc:date>2020-09-16T22:18:25Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Exporting SAS to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684493#M79538</link>
      <description>&lt;P&gt;I fixed the code but could not test it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let today = SEP20;
data _NULL_;
 set Work.Cases;
  by state notsorted;
     if first.state then
        call execute ("proc export data=" || strip(state) || " " ||
        cats('outfile="C:\Users\JPark\Documents\Test\',strip(state),"_&amp;amp;today..xlsx",'" dbms=xlsx replace; run;'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Sep 2020 04:16:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-Exporting-SAS-to-Excel/m-p/684493#M79538</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-09-17T04:16:16Z</dc:date>
    </item>
  </channel>
</rss>

