BookmarkSubscribeRSS Feed
u_chandra
Calcite | Level 5

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()

1 REPLY 1
ballardw
Super User

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);

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 662 views
  • 0 likes
  • 2 in conversation