I have a Base SAS program that imports a 'CSV' file using the 'infile' statement without any issue. I ideally would like to check the variable 'dateto' to see if it matches today's date. If so, proceed to import the file if not stop the import. Below is my current code to import the 'csv' file. I would appreciate any help and I am not a SAS expert. Any simple code\process will be helpful.
I am using SAS version 9.4
/*creates a fileref for the external file*/
Filename ameritot '\\nyadpam\nyadpam_d\apps\CWA_Bondedge_INTMUTUALAMERI_TOT\Bondedge_upload_INTMUTUALAMERITOT.csv';
/* import the external file*/
data bondedge_upload_tot;
%let _EFIERR_ = 0; /* set the ERROR detection macro variable */
infile ameritot delimiter=',' MISSOVER DSD LRECL=32767
FirstOBS=2;
informat Date_To $10. ;
informat CUSIP $14. ;
informat Market_Price $10. ;
informat Portfolio $21. ;
informat Orig_Face $17. ;
informat Actual_Par $18. ;
informat Book_Price $10. ;
informat Security_Type_Description $14. ;
format Date_To $10. ;
format CUSIP $14. ;
format Market_Price $10. ;
format Portfolio $21. ;
format Orig_Face $17. ;
format Actual_Par $18. ;
format Book_Price $10. ;
format Security_Type_Description $14. ;
input
Date_To $
CUSIP $
Market_Price $
Portfolio $
Orig_Face $
Actual_Par $
Book_Price $
Security_Type_Description $
;
if _ERROR_ then call symputx('_EFIERR_',1); /* set ERROR detection macro variable */
run;
Thanks
IC
That is one ugly data step. Looks like it was generated by PROC IMPORT.
You can use the optional CANCEL option on the RUN statement prevent the data step from running. That way you can control wheter it runs without having to learn how to generate code conditionally.
Just read enough of the file to check it you want to really read it or not and set a macro variable. Might be best to make the default be to NOT run the step and only run it when the file pases the test. Then an empty file will not try to be read.
filename ameritot '\\nyadpam\nyadpam_d\apps\CWA_Bondedge_INTMUTUALAMERI_TOT\Bondedge_upload_INTMUTUALAMERITOT.csv';
%let run_cancel=CANCEL ;
data _null_;
infile ameritot firstobs=2 obs=3 dsd ;
input date_to :$10. ;
if input(date_to,??anydtdte.) = today() then call symputx('run_cancel',' ');
run;
data bondedge_upload_tot;
infile ameritot dsd truncover firstobs=2 ;
length Date_To $10 CUSIP $14 Market_Price $10 Portfolio $21
Orig_Face $17 Actual_Par $18 Book_Price $10 Security_Type_Description $14
;
input Date_To -- Security_Type_Description;
run &run_cancel ;
@Ivc wrote:
I have a Base SAS program that imports a 'CSV' file using the 'infile' statement without any issue. I ideally would like to check the variable 'dateto' to see if it matches today's date. If so, proceed to import the file if not stop the import. Below is my current code to import the 'csv' file.
Seems to me this is circular logic. You can't know the value of DATETO until you import the file; but then you only want to import the file if DATETO is today's date, which you won't know until you import the file. Please explain further.
You don't do yourself any favors by reading that variable as character
informat Date_To $10. ;
Since you don't show anything of what that may look like we can't even really suggest how to turn it into a date value to compare to "today".
As @PaigeMiller says, there is not going to be any way to avoid reading the file because the only place the date_to exists is in that file.
Read it. If the "date" doesn't match then you can delete the data set.
If your reason for not wanting to read the file is because it might replace an existing dataset when you don't want it to, then use a different dataset name on the read program, test the value from the data and if it doesn't match today then delete the data set or stop the program.
Reeza,
The date will be constant throughout the file. The idea was to test the date in the file, if it fails then the file would not be imported. I guess I may have to go the route of importing and then check the resulting dataset.
Thanks
Reeza,
I have not develop the macro skill.
Thanks
Reeza,
Here is the version info:
For Base SAS Software ...
Custom version information: 9.4_M2
Image version information: 9.04.01M2P072314
For SAS/ACCESS Interface to ODBC ...
Custom version information: 9.4_M2
Thanks
1. change the first step to read just the date, because it's the first variable this is doable and easy. Also set the obs=2 to read only the first one or two records. I can't recall the interaction between firstobs and OBS so setting it to 2 is safer. (Orange below)
2. Read the date in as a SAS date, not a character so you can test it correctly. (Purple)
3. Create a macro variable from the date to use in the next step (Blue)
4. Test that the macro variable is created (Pink)
5. Macro to conditionally read in data (Red)
6. Execute macro (Green)
HTH, if not, post back exactly the code you ran and the errors you're getting. Untested because of time and no data.
Filename ameritot '\\nyadpam\nyadpam_d\apps\CWA_Bondedge_INTMUTUALAMERI_TOT\Bondedge_upload_INTMUTUALAMERITOT.csv'; /* import the external file*/ data _null_; infile ameritot delimiter=',' MISSOVER DSD LRECL=32767 FirstOBS=2 OBS=2; informat Date_To yymmdd10. ; format Date_To yymmdd10. ; input Date_To $ ; call symputx("dateFromFile", date_to);
run; *test macro variable is created and has a value. Will look like a number in the log; %put &dateFromFile.;
%macro readFile(date_to = ); %if &date_to = %sysfunc(today()) %then %do; *********************** *Import code goes here* ***********************; %end;
%mend;
%readFile(date_to = &dateFromFile.);
@Ivc wrote:
Reeza,
Here is the version info:
For Base SAS Software ...
Custom version information: 9.4_M2
Image version information: 9.04.01M2P072314
For SAS/ACCESS Interface to ODBC ...
Custom version information: 9.4_M2
Thanks
Reeza,
I am attaching the log and the program I ran.
SAS logs (and programs) are just text, no need to attach them. Just use the Insert Code button (looks like {i}) on the menu bar to get a pop-up window where you can paste the lines of text.
Here is the log
You appear to be trying to reference a value you have not read yet.
641 call symputx("dateFromFile", date_to); 642 643 input Date_To $ 644 ;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.