<?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: macro to query each sqlite database (using &amp;amp;amp;filename) in a folder of multiple sqlite dat in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-to-query-each-sqlite-database-using-amp-amp-filename-in-a/m-p/600741#M173704</link>
    <description>&lt;P&gt;Thank you for updating my knowledge of the&amp;nbsp;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: 150%; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;CALL SYMPUTX()&amp;nbsp;&lt;/SPAN&gt; function. That helped.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My final solution is not as automated as I would have liked, but it did the job:&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;** make list of files to process ****;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;filename indata pipe 'dir C:\input_data_directory /b';&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;data file_list;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;length fname $56;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;infile indata truncover; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* infile statement for file names */&amp;nbsp;&amp;nbsp;&lt;BR /&gt;input fname $56.; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; /* read the file names from the directory */&amp;nbsp;&amp;nbsp;&lt;BR /&gt;call symput ('num_files',_n_); &amp;nbsp;/* store the record number in a macro variable */&lt;BR /&gt;run;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;data file_list;&lt;BR /&gt;set&amp;nbsp; file_list;&lt;BR /&gt;attrib scenario length = $10;&lt;BR /&gt;if &amp;nbsp;&amp;nbsp;&amp;nbsp;index(fname, "2015") &amp;nbsp;then scenario = "2015baseCase";&lt;BR /&gt;else if&amp;nbsp;&amp;nbsp;index(fname, "2025") &amp;nbsp;then do;&lt;BR /&gt;&amp;nbsp;if &amp;nbsp;&amp;nbsp;index(fname, "A") &amp;nbsp; &amp;nbsp;then scenario = "2025_A;&lt;BR /&gt;&amp;nbsp;else if index(fname, "B") &amp;nbsp;&amp;nbsp;then scenario = "2025_B";&lt;BR /&gt;&amp;nbsp;else if index(fname, "C") &amp;nbsp;&amp;nbsp;then scenario = "2025C";&lt;BR /&gt;&amp;nbsp;*...add more files...*;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;&lt;BR /&gt;** Set up path to read files from the input data directory ****;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;%let path= C:\input_data_directory;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;** Now iterate through files in input data directory ****;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;%macro macroName;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;%do j=1 %to &amp;amp;num_files ;&lt;BR /&gt;data _null_;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;set file_list;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;if _n_=&amp;amp;j;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;call symputx ('filein',fname);&lt;BR /&gt;call symputx ('scen',scenario);&lt;BR /&gt;%let scen=scenario;&lt;BR /&gt;%let fn=fname;&lt;BR /&gt;run; &lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;/* create the desired SQLITE query */&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;filename QUERY pipe "c:\sqlite\sqlite3.exe &amp;amp;path\FirstPartOfFilename&amp;amp;scen.-LastPartOfFilename.sqlite ""select * from table_of_interest"" ";&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;/* execute the query for database j */&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;data results_ThisDB;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; infile QUERY delimiter='|' missover dsd lrecl=32767;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; length var1 8&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; var2 8;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; input&amp;nbsp; var1 var2 ;&lt;BR /&gt;run;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;/* create an identifier (i.e., scenario) to later identify the source database */&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;data&amp;nbsp;results_ThisDB;&lt;BR /&gt;set &amp;nbsp; results_ThisDB;&lt;BR /&gt;attrib scenario length = $10;&lt;BR /&gt;scenario = "&amp;amp;scen";&lt;BR /&gt;run;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;/* assemble the individual files into a single file */&lt;BR /&gt;%if &amp;amp;j=1 %then %do;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;data &amp;nbsp;results_All;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;set &amp;nbsp;&amp;nbsp; results_ThisDB;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;%else %do;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;data &amp;nbsp;results_All;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;set &amp;nbsp;&amp;nbsp; results_All results_ThisDB;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;run;&lt;BR /&gt;%end; &lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;%end;&amp;nbsp; /* end of do-loop with index j */&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;%mend macroName; &lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;&lt;BR /&gt;%macroName; &lt;/FONT&gt;&lt;/DIV&gt;</description>
    <pubDate>Thu, 31 Oct 2019 16:52:40 GMT</pubDate>
    <dc:creator>mstinson16</dc:creator>
    <dc:date>2019-10-31T16:52:40Z</dc:date>
    <item>
      <title>macro to query each sqlite database (using &amp;amp;filename) in a folder of multiple sqlite databases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-to-query-each-sqlite-database-using-amp-amp-filename-in-a/m-p/598221#M172500</link>
      <description>&lt;DIV&gt;&lt;FONT&gt;Hello, I am trying to use a macro to iterate through multiple sqlite databases that are stored in a folder (the file names are stored in the "filelist" table). The query runs if I use each database name statically, but I'm having trouble using "&amp;amp;" to cycle through all of the files in an automated fashion. Does anyone have a solution to fix this syntax issue? Thank you! Monique&lt;BR /&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;%macro &lt;FONT&gt;macroName&lt;/FONT&gt;;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;%do j=1 %to &amp;amp;num_files ;&lt;BR /&gt;data _null_;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;set file_list;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;if _n_=&amp;amp;j;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;call symput ('filein',fname);&lt;BR /&gt;call symput ('scen',scenario);&lt;BR /&gt;run;&amp;nbsp;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;&lt;BR /&gt;/* THIS WORKS:&lt;BR /&gt;filename QUERY pipe "c:\sqlite\sqlite3.exe C:\data\WF2\wf2\demandTables\chicago2015CT-Demand_updated.sqlite ""select * from freight_trips_with_path"" ";&lt;BR /&gt;*/&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;/* THIS DOES NOT WORK: */&lt;BR /&gt;filename QUERY pipe "c:\sqlite\sqlite3.exe &lt;FONT&gt;C:\data\WF2\wf2\demandTables\&lt;/FONT&gt;&amp;amp;fname ""select * from freight_trips_with_path"" ";&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;%end;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;%mend macroName; &lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;%&lt;FONT&gt;macroName&lt;/FONT&gt;; &lt;/FONT&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 21 Oct 2019 19:17:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-to-query-each-sqlite-database-using-amp-amp-filename-in-a/m-p/598221#M172500</guid>
      <dc:creator>mstinson16</dc:creator>
      <dc:date>2019-10-21T19:17:40Z</dc:date>
    </item>
    <item>
      <title>Re: macro to query each sqlite database (using &amp;amp;filename) in a folder of multiple sqlite dat</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-to-query-each-sqlite-database-using-amp-amp-filename-in-a/m-p/598230#M172506</link>
      <description>&lt;P&gt;What values are setting to the macro variables? If the macro variable FNAME has the value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let fname=chicago2015CT-Demand_updated.sqlite;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then the two FILENAME statements are exactly the same.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you including trailing spaces in the macro variable values by using the old style CALL SYMPUT() function instead of the newer CALL SYMPUTX() function?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 19:36:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-to-query-each-sqlite-database-using-amp-amp-filename-in-a/m-p/598230#M172506</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-21T19:36:36Z</dc:date>
    </item>
    <item>
      <title>Re: macro to query each sqlite database (using &amp;amp;filename) in a folder of multiple sqlite dat</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-to-query-each-sqlite-database-using-amp-amp-filename-in-a/m-p/600741#M173704</link>
      <description>&lt;P&gt;Thank you for updating my knowledge of the&amp;nbsp;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: 150%; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;CALL SYMPUTX()&amp;nbsp;&lt;/SPAN&gt; function. That helped.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My final solution is not as automated as I would have liked, but it did the job:&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;** make list of files to process ****;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;filename indata pipe 'dir C:\input_data_directory /b';&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;data file_list;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;length fname $56;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;infile indata truncover; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* infile statement for file names */&amp;nbsp;&amp;nbsp;&lt;BR /&gt;input fname $56.; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; /* read the file names from the directory */&amp;nbsp;&amp;nbsp;&lt;BR /&gt;call symput ('num_files',_n_); &amp;nbsp;/* store the record number in a macro variable */&lt;BR /&gt;run;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;data file_list;&lt;BR /&gt;set&amp;nbsp; file_list;&lt;BR /&gt;attrib scenario length = $10;&lt;BR /&gt;if &amp;nbsp;&amp;nbsp;&amp;nbsp;index(fname, "2015") &amp;nbsp;then scenario = "2015baseCase";&lt;BR /&gt;else if&amp;nbsp;&amp;nbsp;index(fname, "2025") &amp;nbsp;then do;&lt;BR /&gt;&amp;nbsp;if &amp;nbsp;&amp;nbsp;index(fname, "A") &amp;nbsp; &amp;nbsp;then scenario = "2025_A;&lt;BR /&gt;&amp;nbsp;else if index(fname, "B") &amp;nbsp;&amp;nbsp;then scenario = "2025_B";&lt;BR /&gt;&amp;nbsp;else if index(fname, "C") &amp;nbsp;&amp;nbsp;then scenario = "2025C";&lt;BR /&gt;&amp;nbsp;*...add more files...*;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;&lt;BR /&gt;** Set up path to read files from the input data directory ****;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;%let path= C:\input_data_directory;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;** Now iterate through files in input data directory ****;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;%macro macroName;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;%do j=1 %to &amp;amp;num_files ;&lt;BR /&gt;data _null_;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;set file_list;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;if _n_=&amp;amp;j;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;call symputx ('filein',fname);&lt;BR /&gt;call symputx ('scen',scenario);&lt;BR /&gt;%let scen=scenario;&lt;BR /&gt;%let fn=fname;&lt;BR /&gt;run; &lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;/* create the desired SQLITE query */&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;filename QUERY pipe "c:\sqlite\sqlite3.exe &amp;amp;path\FirstPartOfFilename&amp;amp;scen.-LastPartOfFilename.sqlite ""select * from table_of_interest"" ";&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;/* execute the query for database j */&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;data results_ThisDB;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; infile QUERY delimiter='|' missover dsd lrecl=32767;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; length var1 8&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; var2 8;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; input&amp;nbsp; var1 var2 ;&lt;BR /&gt;run;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;/* create an identifier (i.e., scenario) to later identify the source database */&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;data&amp;nbsp;results_ThisDB;&lt;BR /&gt;set &amp;nbsp; results_ThisDB;&lt;BR /&gt;attrib scenario length = $10;&lt;BR /&gt;scenario = "&amp;amp;scen";&lt;BR /&gt;run;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;/* assemble the individual files into a single file */&lt;BR /&gt;%if &amp;amp;j=1 %then %do;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;data &amp;nbsp;results_All;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;set &amp;nbsp;&amp;nbsp; results_ThisDB;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;%else %do;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;data &amp;nbsp;results_All;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;set &amp;nbsp;&amp;nbsp; results_All results_ThisDB;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;run;&lt;BR /&gt;%end; &lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;%end;&amp;nbsp; /* end of do-loop with index j */&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;%mend macroName; &lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;&lt;BR /&gt;%macroName; &lt;/FONT&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 31 Oct 2019 16:52:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-to-query-each-sqlite-database-using-amp-amp-filename-in-a/m-p/600741#M173704</guid>
      <dc:creator>mstinson16</dc:creator>
      <dc:date>2019-10-31T16:52:40Z</dc:date>
    </item>
  </channel>
</rss>

