Hello
I am using proc append while import CSV files.
What is the reason that proc append is not working?
I am checking and dataset tbl_All was not created?
What is the correct code to do it please?
%macro import_one(YYYYMMDD);
%if %sysfunc(fileexist(/path/Revenue&YYYYMMDD.))
%then %do;
data tbl&YYYYMMDD.;
infile "path/Revenue&YYYYMMDD."
dlm=';' dsd truncover firstobs=2;
input VAR1-VAR59;
run;
%end;
%else %do;
data tbl&YYYYMMDD.;
Format VAR1-VAR59 8.;
stop;
run;
%end;
proc append base=tbl_All data=tbl&YYYYMMDD. force; run; quit;
proc delete data=tbl&YYYYMMDD.;Run;
%mend import_one;
Data datestbl;
input YYYYMMDD $;
call execute('%nrstr(%import_one('!!YYYYMMDD!!'))');
cards;
20191231
20210101
20210102
20210103
20210104
20210105
;
Run;
Maxim 2: Read the Log.
If that does not give you a sufficient clue, post the log here.
You seem to already have a BASE dataset that does not contain your variables. Make sure that this dataset does not exist before you run your code.
Also, while developing/debugging, comment the PROC DELETE, so you can inspect the intermediate files.
But if your goal is to read a sequence of csv files into one dataset, you can do that in a single data step by using the FILEVAR= option of the INFILE statement and a loop for reading each file:
data want;
set datestbl;
fname = "/path/revenue" !! yymmdd;
if fileexist(fname)
then do;
infile dummy filevar=fname dlm="," dsd truncover end=done;
do until (done);
input var1-var59;
output;
end;
end;
run;
To prevent the step from stopping on the first empty file switch from UNTIL() to WHILE().
do while (not done);
The filenames are different here:
%if %sysfunc(fileexist(/path/RevenueA&YYYYMMDD.))
%then %do;
data tbl&YYYYMMDD.;
infile "path/Revenue&YYYYMMDD."
In the INFILE statement, there is no leading slash, and no A before the date, but both are present in the FILEEXIST call.
Thank you, I have changed the tables names because I cannot copy names from my work.
The import is running well .
IF the CSV file exists then a data set is created (with rows and columns).
IF the CSV file doesnt exist then an empty data set is created with 0 rows and columns .
In both cases columns are same (VAR-VAR59 with same attributes: all numerical).
The proc append is not working.
May anyone know why?
@Ronein wrote:
Thank you, I have changed the tables names because I cannot copy names from my work.
The import is running well .
IF the CSV file exists then a data set is created (with rows and columns).
IF the CSV file doesnt exist then an empty data set is created with 0 rows and columns .
In both cases columns are same (VAR-VAR59 with same attributes: all numerical).
The proc append is not working.
May anyone know why?
You need to make sure the empty datasets have the exact same structure as the other datasets.
I showed you how to modify the placement of the %IF block in the middle of the data step so that the empty datasets have the same structure as the other datasets in your other thread.
Log.
I do not see any file extension such as CSV in your FILEEXIST or INFILE statements. Which is pretty suspect.
The import is working well.
The source files are without CSV end .
But they are with delimiter ;
As I said the import is working well but the proc append not
Now this is working,
%macro import_one(YYYYMMDD);
%if %sysfunc(fileexist(/path/Revenue&YYYYMMDD.))
%then %do;
data Revenue;
infile "path/Revenue&YYYYMMDD."
dlm=';' dsd truncover firstobs=2;
input VAR1-VAR59;
run;
%end;
%else %do;
data Revenue;
Format VAR1-VAR59 8.;
stop;
run;
%end;
proc append base=Revenue_All data=Revenue force; run; quit;
%mend import_one;
Data datestbl;
input YYYYMMDD $;
call execute('%nrstr(%import_one('!!YYYYMMDD!!'))');
cards;
20191231
20210101
20210102
20210103
20210104
20210105
;
Run;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.