DATA Step, Macro, Functions and more

infile last file of the month (based on date)

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

infile last file of the month (based on date)

Hi,

 

i want to write a macro that will infile my txt (name of the file is based in a date, example:  aaa_20170929) file on the last working day of the month.

 

For example, last working day of september was 29, last day of october is 31.

 

thank you in advance.


Accepted Solutions
Solution
‎10-04-2017 09:26 AM
Super User
Super User
Posts: 7,981

Re: infile last file of the month (based on date)

So, step one get date value, second step use to import, rough guess:

data _null_;
  prev=intnx('',today(),-1,'end'); /* Moves back a month */
  if weekday(prev)=1 then prev=prev-2;  /* sunday so remove 2 */
  if weekday(prev)=7 then prev=prev-1;  /* sat so -1 */
  call symputx('dt',put(prev,yymmdd10.));
run;

data want;
  infile "c:\abc_&dt..txt";
...
run;

View solution in original post


All Replies
Respected Advisor
Posts: 4,173

Re: infile last file of the month (based on date)

@Jedrzej

Assuming you don't expect us to provide the full code free of charge: What's the question/problem you need help with? What have you done already?

 

In case you don't know how to align a date to the end of month: Look up the intnx() function in the docu.

Contributor
Posts: 22

Re: infile last file of the month (based on date)

i'm getting txt files from monday to friday. I want to infile just the last file that exist of the previous month, every first day of the current month.

So the problem is that the names of files are based on the date that they were generated and if the last day of the month will be Sunday there won't be any file.

I need a macro that will check txt files from the previous month and infile the last one that exist.
Solution
‎10-04-2017 09:26 AM
Super User
Super User
Posts: 7,981

Re: infile last file of the month (based on date)

So, step one get date value, second step use to import, rough guess:

data _null_;
  prev=intnx('',today(),-1,'end'); /* Moves back a month */
  if weekday(prev)=1 then prev=prev-2;  /* sunday so remove 2 */
  if weekday(prev)=7 then prev=prev-1;  /* sat so -1 */
  call symputx('dt',put(prev,yymmdd10.));
run;

data want;
  infile "c:\abc_&dt..txt";
...
run;
Super User
Posts: 10,044

Re: infile last file of the month (based on date)

%let year=2017;
%let month=9;

%let want=%sysfunc(intnx(weekday,%sysfunc(mdy(&month+1,1,&year))-1,0),yymmddn.);

%put &want;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 104 views
  • 1 like
  • 4 in conversation