<?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 IMPORT IF EXISTS AND CREATE EMPTY DATASET IF NOT in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745547#M233716</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Let's talk about the following activity.&lt;/P&gt;
&lt;P&gt;1-My manager send&amp;nbsp; me list of dates&lt;/P&gt;
&lt;P&gt;2-There are CSV files with following name structure RevenueYYYYMMDD (for example:Revenue20210603).&lt;/P&gt;
&lt;P&gt;3-The task is to import the CSV files into SAS data set only for specific dates&lt;/P&gt;
&lt;P&gt;4-I want to import CSV files if they exists&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If a file doesn't exits then I want to create an empty data set (data set without rows).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to ask 2 questions please:&lt;/P&gt;
&lt;P&gt;1-What statements should be added in order to tell SAS to create an empty data set if it doesn't exist?&lt;/P&gt;
&lt;P&gt;(In the current code if the CSV files doesnt exist then SAS data set is not created)&lt;/P&gt;
&lt;P&gt;2-In the real life I have many dates (1000 dates ) and I am not sure if it is good solution to create a macro varaible called vector . What is the way to perform conditional import of CSV files only for dates that exists in dataset?(In this example data set&amp;nbsp;&lt;CODE class=" language-sas"&gt;datestbl define&amp;nbsp;the&amp;nbsp;dates&amp;nbsp;to&amp;nbsp;import)&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data datestbl;
input dates $;
cards;
20210603
20210601
20210528
20210514
;
Run;

PROC SQL noprint;
	select distinct   dates 
         into: vector
	separated by '+'
	from  datestbl
;
QUIT;
%put &amp;amp;vector.;
%let count = %sysfunc(countw(&amp;amp;vector));
%put &amp;amp;count;
 


%macro RRR; 
%do j=1 %to &amp;amp;count.;
%let YYYYMMDD=%scan(&amp;amp;vector.,&amp;amp;j.,+);

%if %sysfunc(fileexist(/path/Revenue&amp;amp;YYYYMMDD.))
%then %do;
options validvarname=v7;
Filename ttt "/path/Revenue&amp;amp;YYYYMMDD.";
proc import datafile=ttt out=Revenue&amp;amp;YYYYMMDD.  dbms=dlm replace ;
getnames=yes;
datarow=2;
delimiter=';'
;
run;
%end;
%end;
%mend RRR;
%RRR; 

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 03 Jun 2021 17:24:08 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2021-06-03T17:24:08Z</dc:date>
    <item>
      <title>IMPORT IF EXISTS AND CREATE EMPTY DATASET IF NOT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745547#M233716</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Let's talk about the following activity.&lt;/P&gt;
&lt;P&gt;1-My manager send&amp;nbsp; me list of dates&lt;/P&gt;
&lt;P&gt;2-There are CSV files with following name structure RevenueYYYYMMDD (for example:Revenue20210603).&lt;/P&gt;
&lt;P&gt;3-The task is to import the CSV files into SAS data set only for specific dates&lt;/P&gt;
&lt;P&gt;4-I want to import CSV files if they exists&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If a file doesn't exits then I want to create an empty data set (data set without rows).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to ask 2 questions please:&lt;/P&gt;
&lt;P&gt;1-What statements should be added in order to tell SAS to create an empty data set if it doesn't exist?&lt;/P&gt;
&lt;P&gt;(In the current code if the CSV files doesnt exist then SAS data set is not created)&lt;/P&gt;
&lt;P&gt;2-In the real life I have many dates (1000 dates ) and I am not sure if it is good solution to create a macro varaible called vector . What is the way to perform conditional import of CSV files only for dates that exists in dataset?(In this example data set&amp;nbsp;&lt;CODE class=" language-sas"&gt;datestbl define&amp;nbsp;the&amp;nbsp;dates&amp;nbsp;to&amp;nbsp;import)&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data datestbl;
input dates $;
cards;
20210603
20210601
20210528
20210514
;
Run;

PROC SQL noprint;
	select distinct   dates 
         into: vector
	separated by '+'
	from  datestbl
;
QUIT;
%put &amp;amp;vector.;
%let count = %sysfunc(countw(&amp;amp;vector));
%put &amp;amp;count;
 


%macro RRR; 
%do j=1 %to &amp;amp;count.;
%let YYYYMMDD=%scan(&amp;amp;vector.,&amp;amp;j.,+);

%if %sysfunc(fileexist(/path/Revenue&amp;amp;YYYYMMDD.))
%then %do;
options validvarname=v7;
Filename ttt "/path/Revenue&amp;amp;YYYYMMDD.";
proc import datafile=ttt out=Revenue&amp;amp;YYYYMMDD.  dbms=dlm replace ;
getnames=yes;
datarow=2;
delimiter=';'
;
run;
%end;
%end;
%mend RRR;
%RRR; 

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 17:24:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745547#M233716</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-06-03T17:24:08Z</dc:date>
    </item>
    <item>
      <title>Re: IMPORT IF EXISTS AND CREATE EMPTY DATASET IF NOT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745549#M233717</link>
      <description>&lt;P&gt;Look up CALL EXECUTE instead of macro loops, much easier to debug and work with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Checks if a file exists (&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n06xm8hwk0t0axn10gj16lfiri43.htm" target="_self"&gt;FILEEXISTS()&lt;/A&gt; function)&amp;nbsp;
&lt;OL&gt;
&lt;LI&gt;If it exists, import the file&lt;/LI&gt;
&lt;LI&gt;If it doesn't, generate empty file&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;Wrap program from #1 into a macro&lt;/LI&gt;
&lt;LI&gt;Use a data step to call macro from CALL EXECUTE passing the list of dates&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 17:32:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745549#M233717</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-03T17:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: IMPORT IF EXISTS AND CREATE EMPTY DATASET IF NOT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745555#M233720</link>
      <description>&lt;P&gt;First of all, do not use PROC IMPORT for CSV files, especially not if you need to combine/compare them later.&lt;/P&gt;
&lt;P&gt;Write a macro that accepts a datestring as argument:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro import_one(date);
%if %sysfunc(fileexist(path/revenue&amp;amp;date..csv))
%then %do;
data revenue&amp;amp;date.;
infile "/path/revenue&amp;amp;date..csv))" dlm=',' dsd truncover;
length /* insert variable definitions here */;
input /* variable list as above */;
run;
%end;
%else %do;
data revenue&amp;amp;date.;
length /* same as above */;
run;
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then call this from the data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data datestbl;
input dates $;
call execute('%nrstr(%import_one('!!date!!'))');
cards;
20210603
20210601
20210528
20210514
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 17:39:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745555#M233720</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-03T17:39:52Z</dc:date>
    </item>
    <item>
      <title>Re: IMPORT IF EXISTS AND CREATE EMPTY DATASET IF NOT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745556#M233721</link>
      <description>&lt;P&gt;Do you have to use PROC IMPORT?&amp;nbsp; Do you not know what variables are in the CSV files?&lt;/P&gt;
&lt;P&gt;It is pretty simple to just read all of the files in one data step instead of making multiple datasets.&lt;/P&gt;
&lt;P&gt;Just replace the part in inner most DO loop with the right INPUT statement for how your CSV files are defined.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datestbl;
  input dates $;
cards;
20210603
20210601
20210528
20210514
;

data revenue ;
  set datestbl;
  length filename $200;
  filename = cats('/path/Revenue',dates,'.csv');
  if fileexist(filename) then do;
    infile csv dsd dlm=';' truncover firstobs=2 end=eof filevar=filename;
    do while(not eof);
       input .... ;
       output;
    end;
  end;
  else do;
     put filename= 'does not exist';
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Jun 2021 17:40:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745556#M233721</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-03T17:40:36Z</dc:date>
    </item>
    <item>
      <title>Re: IMPORT IF EXISTS AND CREATE EMPTY DATASET IF NOT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745568#M233727</link>
      <description>I am not sure if it CSV.&lt;BR /&gt;This code import the file correctly and there are 59 columns called:Var1.Var2,...Var59.&lt;BR /&gt;&lt;BR /&gt;%let YYYYMMDD=20210601;&lt;BR /&gt;proc import datafile="/path/Revenue&amp;amp;YYYYMMDD."&lt;BR /&gt;out=tbl&amp;amp;YYYYMMDD. dbms=dlm replace ;&lt;BR /&gt;getnames=no;&lt;BR /&gt;datarow=2;&lt;BR /&gt;delimiter=';'&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;However,this code is not working well and a data set called Revnue was created with one column called  dates with 0 rows&lt;BR /&gt;data datestbl;&lt;BR /&gt;  input dates $;&lt;BR /&gt;cards;&lt;BR /&gt;20210601&lt;BR /&gt;;&lt;BR /&gt;data revenue ;&lt;BR /&gt;  set datestbl;&lt;BR /&gt;  length filename $200;&lt;BR /&gt;  filename = cats('/path/Revenue',dates,'.csv');&lt;BR /&gt;  if fileexist(filename) then do;&lt;BR /&gt;    infile csv dsd dlm=';' truncover firstobs=2 end=eof filevar=filename;&lt;BR /&gt;    do while(not eof);&lt;BR /&gt;       output;&lt;BR /&gt;    end;&lt;BR /&gt;  end;&lt;BR /&gt;  else do;&lt;BR /&gt;     put filename= 'does not exist';&lt;BR /&gt;  end;&lt;BR /&gt;run;</description>
      <pubDate>Thu, 03 Jun 2021 18:10:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745568#M233727</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-06-03T18:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: IMPORT IF EXISTS AND CREATE EMPTY DATASET IF NOT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745570#M233728</link>
      <description>&lt;P&gt;You did not tell it read any variables from the CSV file.&lt;/P&gt;
&lt;P&gt;A CSV file is not some mythical creature from the dark lagoon.&amp;nbsp; It is just a text file.&amp;nbsp; Open the text file and look at it. If you don't have any documentation from the source on what the variables are they should be pretty clear from the column headers (first row) and the types of values they contain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to read 59 character variables with maximum length of 20 bytes each your inner DO loop could be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do while(not eof);
  length var1-var59 $20 ;
  input var1-var59 ;
  output;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Jun 2021 18:15:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745570#M233728</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-03T18:15:40Z</dc:date>
    </item>
    <item>
      <title>Re: IMPORT IF EXISTS AND CREATE EMPTY DATASET IF NOT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745575#M233733</link>
      <description>&lt;P&gt;Okay ,now it is working well but there is another problem.&lt;/P&gt;
&lt;P&gt;The name of the data set that I create should include the date on it (for example:Revenue20210601)&lt;/P&gt;
&lt;P&gt;What is the way to add date to data set name here?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datestbl;
  input dates $;
cards;
20210601
20210602
20210603
;
Run;
data cats(revenue,dates) ;
set datestbl;
length filename $200;
filename = cats("/path/Revenue",dates);
if fileexist(filename) then do;
    infile csv dsd dlm=';' truncover firstobs=2 end=eof filevar=filename;
do while(not eof);
  input var1-var59 ;
  output;
end;
  end;
  else do;
     put filename= 'does not exist';
  end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Jun 2021 18:30:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745575#M233733</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-06-03T18:30:04Z</dc:date>
    </item>
    <item>
      <title>Re: IMPORT IF EXISTS AND CREATE EMPTY DATASET IF NOT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745578#M233736</link>
      <description>The name of the data set should contain the date.&lt;BR /&gt;In this  example there are potential 4 data sets to create but you gave one name "revenue"</description>
      <pubDate>Thu, 03 Jun 2021 18:44:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745578#M233736</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-06-03T18:44:28Z</dc:date>
    </item>
    <item>
      <title>Re: IMPORT IF EXISTS AND CREATE EMPTY DATASET IF NOT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745591#M233745</link>
      <description>&lt;P&gt;Why would you want to make a separate dataset for each date?&lt;/P&gt;
&lt;P&gt;If you want to only look at some of the data for a particular analysis just use a WHERE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 19:20:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745591#M233745</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-03T19:20:42Z</dc:date>
    </item>
    <item>
      <title>Re: IMPORT IF EXISTS AND CREATE EMPTY DATASET IF NOT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745823#M233876</link>
      <description>&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jun 2021 15:47:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745823#M233876</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-06-04T15:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: IMPORT IF EXISTS AND CREATE EMPTY DATASET IF NOT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745824#M233877</link>
      <description>&lt;P&gt;Thank you, May you show the full code please of import multiple CSV files in your approach?&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jun 2021 15:51:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745824#M233877</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-06-04T15:51:26Z</dc:date>
    </item>
    <item>
      <title>Re: IMPORT IF EXISTS AND CREATE EMPTY DATASET IF NOT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745825#M233878</link>
      <description>&lt;P&gt;Show me the what columns are in your CSV files and how you want them defined as variables in the resulting SAS dataset.&lt;/P&gt;
&lt;P&gt;For example you might have data like:&lt;/P&gt;
&lt;PRE&gt;Name,Sex,Age,Height,Weight
Alfred,M,14,69,112.5
Alice,F,13,56.5,84
Barbara,F,13,65.3,98
&lt;/PRE&gt;
&lt;P&gt;So you would want to define the variables like and read them in using code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length name $7 sex $1 age height weight 8 ;
input name -- weight;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Jun 2021 15:59:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745825#M233878</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-04T15:59:43Z</dc:date>
    </item>
    <item>
      <title>Re: IMPORT IF EXISTS AND CREATE EMPTY DATASET IF NOT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745831#M233881</link>
      <description>&lt;P&gt;The method to import multiple files into one dataset in one data step is shown&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/proc-append-while-import-CSV-files/m-p/745738/highlight/true#M233823" target="_blank" rel="noopener"&gt;here&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jun 2021 16:09:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IMPORT-IF-EXISTS-AND-CREATE-EMPTY-DATASET-IF-NOT/m-p/745831#M233881</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-04T16:09:56Z</dc:date>
    </item>
  </channel>
</rss>

