BookmarkSubscribeRSS Feed
annypanny
Quartz | Level 8

Hi community, my yesterday's question was a bit confusing actually it was for me too.

Now I have to a extract the latest file from a location and store it to a dataset.

suppose I have a input folder location "/sas/sasdataset/lev1/" and in that location there are multiple .txt files e.g. user20200414.txt, user20200415.txt, Userxyz.txt are present, I have to extract the latest or any of the  file into a dataset. How can I do? Please if anybody a code example.

I am using below code for the latest file but the observations in variable date is null in the output of this code:

 

</filename users pipe "/sas/sasdata/Lev1/External_Files/Cofunds_Files/mi/";
data files;
infile users truncover;
input filename $100.;
date = input(scan(scan(scan(filename,-1,'/'),1,'.'),3,'_'), mmddyy8.);
run;

proc sort data=files;
by descending date;
run;

data _null_;
set files;
call symput('infile',filename);
stop;
run;/>

Now if I get the latest how can I extract it into dataset?

 

EDITED

 

9 REPLIES 9
Kurt_Bremser
Super User

Try to be as clear and precise with your questions.

What do you mean by "latest"? Is it the file with the most recent modification timestamp, does it have a timestamp coded into the filename (if yes, how?), or do you have a data item in the file itself (e. g. in a header) that defines a point in time?

Your directory name suggests a UNIX system for your SAS server. Is this true?

Do you have XCMD enabled? Run

proc options option=xcmd;
run;

to determine that.

annypanny
Quartz | Level 8
suppose there are files like user20200415.txt, user20200414.txt, Userxyx.txt how can I extract any of this files and convert it into a dataset?
Kurt_Bremser
Super User

See this example code for retrieving all the filenames in a directory (tested on SAS University Edition):

data files;
length location $8;
rc = filename(location,"/folders/myfolders");
did = dopen(location);
do i = 1 to dnum(did);
  name = dread(did,i);
  output;
end;
rc = dclose(did);
rc = filename(location);
keep name;
run;
annypanny
Quartz | Level 8
Thanks this is working and my first part of the problem is solved, now how can I convert "any" one of the file from that location in dataset after extracting those files
andreas_lds
Jade | Level 19

@annypanny wrote:
suppose there are files like user20200415.txt, user20200414.txt, Userxyx.txt how can I extract any of this files and convert it into a dataset?

I have to repeat what @Kurt_Bremser already asked for: "Try to be as clear and precise with your questions."

So, do you want to import all files or just the latest? If you want to import all files: are all files in the same structure? If yes: do you want one dataset per file or one dataset with the contents of all files?

annypanny
Quartz | Level 8
yes, and all are in same structure e.g., ONLINEUSER20200415.txt, ONLINEUSER20200414.txt how can I find the latest one and How can I extract any one of them into a dataset (say example of file which is also latest according to date ONLINEUSER20200415.txt)
andreas_lds
Jade | Level 19

You will have to write a data-step reading the file. I recommend starting with one file, which name to provide in the infile-statement, without any steps automatically selecting the file. The input-statement depends on the structure of the file.

annypanny
Quartz | Level 8
please would you mind to answer on the given link as it is more clear question then my previous ones:
https://communities.sas.com/t5/SAS-Programming/How-to-extract-an-external-file-into-a-sas-dataset/td...

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 16. 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
  • 9 replies
  • 1065 views
  • 2 likes
  • 3 in conversation