Dear All
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.
ZLGOPAR.txt.20140404_205224
VLSNHMONFCW.txt.20140404_212853
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);
data file1;
infile "&Path\file*.txt" filename = dfnVAR eov = first_this_file end = done;
Input ...................................................................................................
;
if _n_ = 1 or first_this_file then
Put 'Start reading ' dfnVAR=;
run;
IF MONTHint < 4 THEN QRT = 2;
ELSE IF MONTHint < 7 THEN QRT = 3;
ELSE IF MONTHint < 10 THEN QRT = 4;
ELSE QRT = 1;
%mend readindata;
%readindata()
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.);
month=month(filedate);
QRT = qtr(filedate);
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.