BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Rixile106
Fluorite | Level 6

Good expects.

i have the following txt files which are received in a weekly basis (every Monday).

Rixile106_1-1655216440537.png

 

i use the import statement to convert the file to sas data format ,however i need to hard code the date every time (every Monday) a new file gets generated, is there a way/ function used in sas that will enable me not to hard code the date 

PROC IMPORT 
     OUT= WORK.GLACNT_IMPORT
     DATAFILE= "/SAS/data/Text/data/GLACNT.20220523.TXT"
     DBMS=DLM REPLACE;
     DELIMITER='|'
     DSD
     LRCEL=12
    GETNAMES=YES;
     DATAROW=2;
RUN;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You can use a macro variable to build the filename.

So set the value into the macro variable and replace the date-like string in the name with a reference to macro variable.

Note the extra period.  The first period marks the end of the name of the macro variable. The second period is the one that becomes part of the filename.

%let today=20220523 ;
...
   DATAFILE= "/SAS/data/Text/data/GLACNT.&today..TXT"
...

So assuming that string represents a date in YYYYMMDD order you can generate such a string using the YYMMDDN format.

So if you just want today's date:

%let today=%sysfunc(date(),yymmddn8.);

If you want some other date then calculate that date instead.  For example if the want the most recent Monday you could use INTNX() with WEEK.2 interval.

%let monday=%sysfunc(intnx(week.2,%sysfunc(date()),0),yymmddn8.);

And then use &MONDAY in making the filename.

...
   DATAFILE= "/SAS/data/Text/data/GLACNT.&monday..TXT"
...

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

What is the question? You have text files, what do you want to do with them?

--
Paige Miller
Rixile106
Fluorite | Level 6

i want a function that will enables me to stop hard coding the date everytime a new files gets generated e.g &Yday.

PaigeMiller
Diamond | Level 26

You should explain more about how any code that is written would know what days/file names to look for. I'm not asking for SAS code, I am asking you to explain in words what the rules are that this code must follow so that the right files can be read in.

 

Or I could guess, but usually that's not as good an idea as you explaining.

--
Paige Miller
Tom
Super User Tom
Super User

You can use a macro variable to build the filename.

So set the value into the macro variable and replace the date-like string in the name with a reference to macro variable.

Note the extra period.  The first period marks the end of the name of the macro variable. The second period is the one that becomes part of the filename.

%let today=20220523 ;
...
   DATAFILE= "/SAS/data/Text/data/GLACNT.&today..TXT"
...

So assuming that string represents a date in YYYYMMDD order you can generate such a string using the YYMMDDN format.

So if you just want today's date:

%let today=%sysfunc(date(),yymmddn8.);

If you want some other date then calculate that date instead.  For example if the want the most recent Monday you could use INTNX() with WEEK.2 interval.

%let monday=%sysfunc(intnx(week.2,%sysfunc(date()),0),yymmddn8.);

And then use &MONDAY in making the filename.

...
   DATAFILE= "/SAS/data/Text/data/GLACNT.&monday..TXT"
...
ballardw
Super User

If these files are supposed to be of the same structure you really should use  Data step code to read the files so that the variables have the same characteristics. Every time you run Proc Import it makes guesses as to the type and length of variables and that may change depending on the contents.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 620 views
  • 1 like
  • 4 in conversation