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
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.
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 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?
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.
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!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: