- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I am trying to import csv file into sas based on weekday.
If its Monday, it has to import files of Saturday, Sunday and Monday and concatenate them, and rest of the days it has to import files of that day.
csv files have name convention of "file-20220203.csv"
I tried writing a If-Then-Else condition, where if weekday is 2(Monday) then import files of today, yesterday and daybefore yesterday, else import file of that day. But its not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Please post the complete SAS log of your program. Without seeing what you've done we can't help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This is the code I tried.
%let weekdy = weekday(today());
%let day = %sysfunc(putn(%eval(%sysfunc(today())), yymmddn8.));
%let ysday = %sysfunc(putn(%eval(%sysfunc(today())-1), yymmddn8.));
%let dbysday = %sysfunc(putn(%eval(%sysfunc(today())-2), yymmddn8.));
%let loc=/location/Data;
data _null_;
d=&weekdy.;
if d=2 then do;
proc import datafile="/&loc./file-&day..csv"
dbms=csv
out=data_1
replace;
run;
/*yesterday_file*/
proc import datafile="/&loc./file-&ysday..csv"
dbms=csv
out=data_11
replace;
run;
/*daybefore_file*/
proc import datafile="/&loc./file-&dbysday..csv"
dbms=csv
out=data_12
replace;
run;
/*concoat_files*/
data data1;
set work.data_1 work.data_11 work.data_12;
end;
else do;
proc import datafile="/&loc./file-&day..csv"
dbms=csv
out=data_1
replace;
run;
data data1;
set work.data_1;
end;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
How much help do you want?
You're fairly close but you need to use macro logic to break up those steps.
Change your IF/THEN to %IF/%THEN and the corresponding %END.
Some tutorials are below:
UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/
Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
Examples of common macro usage
https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What about public holidays?
Below one way to go that doesn't require macros.
1. Create sample files
/* create sample files */
%let root_path=%sysfunc(pathname(work));
data check;
length file_nm $400 cmd $500;
do dt='03feb2022'd to '08feb2022'd;
file_nm=cats("&root_path",'\file-',put(dt,yymmddn8.),'.csv');
output;
cmd='proc export data=sashelp.class dbms=csv replace';
cmd=catx(' ',cmd,cats("outfile='",file_nm,"'"),';run;');
call execute( cmd );
end;
run;
2. Read files based on current system date when job executes
/* derive names of desired files based on system date */;
/*%let root_path=c:\temp;*/
%let root_path=%sysfunc(pathname(work));
data source_files;
length file_name $300;
stop_dt=today();
start_dt=intnx('weekday12w',stop_dt,0);
do dates=start_dt to stop_dt;
file_name=cats("&root_path\file-",put(dates,yymmddn8.),".csv");
output;
end;
keep file_name;
run;
/* read external file(s) into SAS */;
data want;
set source_files(keep=file_name);
readFile=file_name;
do until(done);
infile dummy dlm=',' dsd truncover filevar=readFile end=done firstobs=2;
input Name:$8. Sex:$1. Age :32. Height:32. Weight:32. ;
output;
end;
run;
If you also need to build for public holidays then the only bit you need to change is the logic that creates table source_files.
To create the target variables Proc Import has to guess what data types and lengths to use based on the text in the source file. For this reason Proc Import can't return stable results.
For example if for one variable the longest source string has 7 characters then Proc Import will create this variable as character with a length of 7 - but if on another day the longest source string has 8 characters the variable will have a length of 8.
You want to read multiple files on Monday's. If using Proc Import multiple times you can easily end up with tables that have the same variable with different lengths - and then depending on the order how you concatenate these tables into one you could end-up with string truncations.
Proc Import is a quick way to read external data for some one-off task but it's much better to "know your data" and use a SAS data step for anything regular.