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

Hello!

How can i define dynamic html file using filename statement? It's supposed to be that file.html will be always with different name. Also there will be only the one html file in a folder every time.

Here is the code: 

filename dir 'path/file.html'; /*currently it's a constant*/

data _null_;
infile dir recfm = n irecl = 32767;
input col1 $32767.;
file "~/file.text";
put col1 $32767.;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ
Hi:
So, how do you know what FILE.HTML to open? Do you pick the one with the most newest date? or the oldest date? or some other criteria, like the word "final" in the name?
If you have some logic that you use to manually pick the name, another approach would be to read the folder where the HTML file is stored and pipe the directory output from the folder to a file that you can read with SAS. Assuming the HTML file you want to read is the one with the most recent date, you should be able to use Program logic to get that name and create a macro variable from that name. I believe there have been several examples of getting the names of all the files in a folder already posted in the Forums.
But even simpler would be to ask your colleagues to create the file with a unique name -- something like:
file_15APR2022.html -- I realize that naming convention would only work if you are running your program on the same date that they are creating the HTML file, but there are other ways to manipulate dates with macro variables that might work.
You have such an open-ended question, we really need some more context around how you decide to pick THAT HTML file out of the folder that contains multiple HTML files.
Cynthia

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

How would the programmer know what the different file name is each time? 

--
Paige Miller
PaigeMiller
Diamond | Level 26

Adding ... it looks like you are reading an html file, and writing the unchanged text in the html file out to a .txt file.

 

Why don't you just rename the file from .html to .txt ?

--
Paige Miller
Cynthia_sas
SAS Super FREQ
Hi:
I'm not clear on what you need.

With the HTML file in a FILENAME statement and then using that file reference in the INFILE statement, it appears that you're reading IN the HTML file in order to CREATE file.text (on the file statement. So you're not showing the process that creates FILE.HTML, you're showing the process that creates FILE.TEXT. So, how does FILE.HTML get created?

Do you want the dynamic name in FILE.HTML (in which case you've got to show the code that creates FILE.HTML).

Or, do you want the dynamic name in FILE.TXT (in which case, all you need is a macro variable with a unique number -- I generally make a macro variable from using the TIME() function for situations like this).
Cynthia
A1ex777
Fluorite | Level 6

@Cynthia_sas thank you for detailed answer.

 

Do you want the dynamic name in FILE.HTML (in which case you've got to show the code that creates FILE.HTML) - there's no code that creates file.html. I get this  html files from my colleagues who makes html coding and this html up for email template.

 

The task is parse some data from this html's files and store it in some ref table. Therefore i wrote code that automatically process it but i faced with the only problem that i mentioned on my initial message that will be different names of html files in same folder. At the moment it means i have to change the name of html file in the filename statement manually every time.

Cynthia_sas
SAS Super FREQ
Hi:
So, how do you know what FILE.HTML to open? Do you pick the one with the most newest date? or the oldest date? or some other criteria, like the word "final" in the name?
If you have some logic that you use to manually pick the name, another approach would be to read the folder where the HTML file is stored and pipe the directory output from the folder to a file that you can read with SAS. Assuming the HTML file you want to read is the one with the most recent date, you should be able to use Program logic to get that name and create a macro variable from that name. I believe there have been several examples of getting the names of all the files in a folder already posted in the Forums.
But even simpler would be to ask your colleagues to create the file with a unique name -- something like:
file_15APR2022.html -- I realize that naming convention would only work if you are running your program on the same date that they are creating the HTML file, but there are other ways to manipulate dates with macro variables that might work.
You have such an open-ended question, we really need some more context around how you decide to pick THAT HTML file out of the folder that contains multiple HTML files.
Cynthia

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1260 views
  • 1 like
  • 3 in conversation