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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1333 views
  • 1 like
  • 4 in conversation