08-28-2014 12:49 PM
I am trying to create a macro with wild card that can dynamically read a series of raw data files (txt data files). The raw data files have name (letters) with .txt extension followed by date_timestamp. The following samples represnt the raw data files.
I have 50-60 raw data files like these. I tried to write a read in macro but it is not working. I have used filename = dfnVAR (in the inflie statement) whose length I want to use to extract Month, Year and FY etc as you can see below.
Please take a look at what I wrote and help me resolve this issue. I am not sure if my infile statement is correct. Anyway, the idea is to be able to read all the raw datafiles dynamically by the macro. I would be grateful to your kind assistance and support.
%LET Path = H:\STARS\Mar\FL;
%LET dfnLEN = LENGTH(dfnVAR);
%LET dfnSTART = dfnLEN – 15;
%LET MONTH = substr(dfnVAR, dfnSTART + 5, 2);
%LET FYread = substr(dfnVAR, dfnSTART + 3, 2);
%LET FYint = input(FYread, 2.);
%LET MONTHint = input(MONTH, 2.);
IF MONTHint > 9 THEN FYint = FYint + 1;
%LET FYstr = put(FYint, hex2.);
%LET FY = CAT(“FY”, FYstr);
%LET QRT = 0;
%LET QRT_YR_FULL = CAT(“Q”, QRT, “FY20”, FYstr);
%macro readindata(Table_In, Finalout, Month);
infile "&Path\file*.txt" filename = dfnVAR eov = first_this_file end = done;
if _n_ = 1 or first_this_file then
Put 'Start reading ' dfnVAR=;
IF MONTHint < 4 THEN QRT = 2;
ELSE IF MONTHint < 7 THEN QRT = 3;
ELSE IF MONTHint < 10 THEN QRT = 4;
ELSE QRT = 1;
08-28-2014 02:27 PM
The code looks like there may be confusion between the macro language and data step programming.
If you want to parse the information out of the automatic filename variable dfnvar the use do not use macro statements at all.
You may be able to use this code to read the value of the date part from the file name into a SAS date variable which is much more flexible in the data step and no macro needed:
FileDate = input(scan(dfnvar,2,'._'),yymmdd8.);
QRT = qtr(filedate);