Parse file name with SAS
I have a directory in which every week there is a new file created. names are like below:
file_w1.csv
file_w2.csv
file_w3.csv
What I need to do is pick up the latest file (based on modified date), then parse the 2 characters just before the file extension.
So in this case, I want 'w3' because I want to use this to know which week I am reporting for.
How can I do this in SAS?
sas
add a comment |
I have a directory in which every week there is a new file created. names are like below:
file_w1.csv
file_w2.csv
file_w3.csv
What I need to do is pick up the latest file (based on modified date), then parse the 2 characters just before the file extension.
So in this case, I want 'w3' because I want to use this to know which week I am reporting for.
How can I do this in SAS?
sas
read in the list of files, find the latest one, import it. Exactly how depends on your OS which you did not specify. communities.sas.com/t5/SAS-Programming/…
– Reeza
Nov 22 at 22:02
Please remember to show what you tried How to Ask
– Reeza
Nov 22 at 22:03
add a comment |
I have a directory in which every week there is a new file created. names are like below:
file_w1.csv
file_w2.csv
file_w3.csv
What I need to do is pick up the latest file (based on modified date), then parse the 2 characters just before the file extension.
So in this case, I want 'w3' because I want to use this to know which week I am reporting for.
How can I do this in SAS?
sas
I have a directory in which every week there is a new file created. names are like below:
file_w1.csv
file_w2.csv
file_w3.csv
What I need to do is pick up the latest file (based on modified date), then parse the 2 characters just before the file extension.
So in this case, I want 'w3' because I want to use this to know which week I am reporting for.
How can I do this in SAS?
sas
sas
asked Nov 22 at 21:30
Victor
6,69149158319
6,69149158319
read in the list of files, find the latest one, import it. Exactly how depends on your OS which you did not specify. communities.sas.com/t5/SAS-Programming/…
– Reeza
Nov 22 at 22:02
Please remember to show what you tried How to Ask
– Reeza
Nov 22 at 22:03
add a comment |
read in the list of files, find the latest one, import it. Exactly how depends on your OS which you did not specify. communities.sas.com/t5/SAS-Programming/…
– Reeza
Nov 22 at 22:02
Please remember to show what you tried How to Ask
– Reeza
Nov 22 at 22:03
read in the list of files, find the latest one, import it. Exactly how depends on your OS which you did not specify. communities.sas.com/t5/SAS-Programming/…
– Reeza
Nov 22 at 22:02
read in the list of files, find the latest one, import it. Exactly how depends on your OS which you did not specify. communities.sas.com/t5/SAS-Programming/…
– Reeza
Nov 22 at 22:02
Please remember to show what you tried How to Ask
– Reeza
Nov 22 at 22:03
Please remember to show what you tried How to Ask
– Reeza
Nov 22 at 22:03
add a comment |
1 Answer
1
active
oldest
votes
An operating system independent technique would use SAS External File functions such as dopen
, fopen
and finfo
to obtain information about a folder and it's items.
Consider this sample code that does a 'full dump' of available information whilst parsing C:Temp
on a Windows machine:
data _null_;
length dfileref fileref $8 folder $200;
rc = filename (dfileref, 'C:Temp');
did = dopen(dfileref);
if did then do;
do index = 1 to doptnum(did);
featurename = doptname(did,index);
featurevalue = dinfo(did,featurename);
put index= featurename= featurevalue=;
if featurename = 'Directory' then folder = featurevalue;
end;
do dindex = 1 to dnum(did);
entryname = dread(did,dindex);
put dindex= entryname=;
rc = filename(fileref, cats(folder, '/', entryname));
fid = fopen (fileref); * if entry is another folder fid will be 0;
if fid then do;
do findex = 1 to foptnum(fid);
featurename = foptname(fid, findex);
featurevalue = finfo(fid, featurename);
put +2 findex= featurename= featurevalue=;
end;
fid = fclose(fid);
end;
rc = filename(fileref);
end;
did = dclose(did);
end;
rc = filename (dfileref);
run;
After examining the log you can pare down the code needed to gather specific desired information into a data set. You can then use SQL queries to further act upon the data:
data csv_files(keep=fullname lastmod where=(fullname like '%.csv'));
length dfileref fileref $8 folder $200;
folder = 'C:Temp';
rc = filename (dfileref, folder);
did = dopen(dfileref);
if did then do;
do dindex = 1 to dnum(did);
entryname = dread(did,dindex);
rc = filename(fileref, cats(folder, '/', entryname));
fid = fopen (fileref);
if fid then do;
fullname = finfo(fid,'Filename');
lastmod = input(finfo(fid,'Last Modified'), datetime18.); format lastmod datetime18.;
output;
fid = fclose(fid);
end;
rc = filename(fileref);
end;
did = dclose(did);
end;
rc = filename (dfileref);
run;
proc sql;
create table csv_newest as
select *, scan(scan(fullname,-1,'_'),1,'.') as tag
from csv_files
where prxmatch ('/_.+.csv$/', fullname)
having lastmod = max(lastmod)
;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438167%2fparse-file-name-with-sas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
An operating system independent technique would use SAS External File functions such as dopen
, fopen
and finfo
to obtain information about a folder and it's items.
Consider this sample code that does a 'full dump' of available information whilst parsing C:Temp
on a Windows machine:
data _null_;
length dfileref fileref $8 folder $200;
rc = filename (dfileref, 'C:Temp');
did = dopen(dfileref);
if did then do;
do index = 1 to doptnum(did);
featurename = doptname(did,index);
featurevalue = dinfo(did,featurename);
put index= featurename= featurevalue=;
if featurename = 'Directory' then folder = featurevalue;
end;
do dindex = 1 to dnum(did);
entryname = dread(did,dindex);
put dindex= entryname=;
rc = filename(fileref, cats(folder, '/', entryname));
fid = fopen (fileref); * if entry is another folder fid will be 0;
if fid then do;
do findex = 1 to foptnum(fid);
featurename = foptname(fid, findex);
featurevalue = finfo(fid, featurename);
put +2 findex= featurename= featurevalue=;
end;
fid = fclose(fid);
end;
rc = filename(fileref);
end;
did = dclose(did);
end;
rc = filename (dfileref);
run;
After examining the log you can pare down the code needed to gather specific desired information into a data set. You can then use SQL queries to further act upon the data:
data csv_files(keep=fullname lastmod where=(fullname like '%.csv'));
length dfileref fileref $8 folder $200;
folder = 'C:Temp';
rc = filename (dfileref, folder);
did = dopen(dfileref);
if did then do;
do dindex = 1 to dnum(did);
entryname = dread(did,dindex);
rc = filename(fileref, cats(folder, '/', entryname));
fid = fopen (fileref);
if fid then do;
fullname = finfo(fid,'Filename');
lastmod = input(finfo(fid,'Last Modified'), datetime18.); format lastmod datetime18.;
output;
fid = fclose(fid);
end;
rc = filename(fileref);
end;
did = dclose(did);
end;
rc = filename (dfileref);
run;
proc sql;
create table csv_newest as
select *, scan(scan(fullname,-1,'_'),1,'.') as tag
from csv_files
where prxmatch ('/_.+.csv$/', fullname)
having lastmod = max(lastmod)
;
add a comment |
An operating system independent technique would use SAS External File functions such as dopen
, fopen
and finfo
to obtain information about a folder and it's items.
Consider this sample code that does a 'full dump' of available information whilst parsing C:Temp
on a Windows machine:
data _null_;
length dfileref fileref $8 folder $200;
rc = filename (dfileref, 'C:Temp');
did = dopen(dfileref);
if did then do;
do index = 1 to doptnum(did);
featurename = doptname(did,index);
featurevalue = dinfo(did,featurename);
put index= featurename= featurevalue=;
if featurename = 'Directory' then folder = featurevalue;
end;
do dindex = 1 to dnum(did);
entryname = dread(did,dindex);
put dindex= entryname=;
rc = filename(fileref, cats(folder, '/', entryname));
fid = fopen (fileref); * if entry is another folder fid will be 0;
if fid then do;
do findex = 1 to foptnum(fid);
featurename = foptname(fid, findex);
featurevalue = finfo(fid, featurename);
put +2 findex= featurename= featurevalue=;
end;
fid = fclose(fid);
end;
rc = filename(fileref);
end;
did = dclose(did);
end;
rc = filename (dfileref);
run;
After examining the log you can pare down the code needed to gather specific desired information into a data set. You can then use SQL queries to further act upon the data:
data csv_files(keep=fullname lastmod where=(fullname like '%.csv'));
length dfileref fileref $8 folder $200;
folder = 'C:Temp';
rc = filename (dfileref, folder);
did = dopen(dfileref);
if did then do;
do dindex = 1 to dnum(did);
entryname = dread(did,dindex);
rc = filename(fileref, cats(folder, '/', entryname));
fid = fopen (fileref);
if fid then do;
fullname = finfo(fid,'Filename');
lastmod = input(finfo(fid,'Last Modified'), datetime18.); format lastmod datetime18.;
output;
fid = fclose(fid);
end;
rc = filename(fileref);
end;
did = dclose(did);
end;
rc = filename (dfileref);
run;
proc sql;
create table csv_newest as
select *, scan(scan(fullname,-1,'_'),1,'.') as tag
from csv_files
where prxmatch ('/_.+.csv$/', fullname)
having lastmod = max(lastmod)
;
add a comment |
An operating system independent technique would use SAS External File functions such as dopen
, fopen
and finfo
to obtain information about a folder and it's items.
Consider this sample code that does a 'full dump' of available information whilst parsing C:Temp
on a Windows machine:
data _null_;
length dfileref fileref $8 folder $200;
rc = filename (dfileref, 'C:Temp');
did = dopen(dfileref);
if did then do;
do index = 1 to doptnum(did);
featurename = doptname(did,index);
featurevalue = dinfo(did,featurename);
put index= featurename= featurevalue=;
if featurename = 'Directory' then folder = featurevalue;
end;
do dindex = 1 to dnum(did);
entryname = dread(did,dindex);
put dindex= entryname=;
rc = filename(fileref, cats(folder, '/', entryname));
fid = fopen (fileref); * if entry is another folder fid will be 0;
if fid then do;
do findex = 1 to foptnum(fid);
featurename = foptname(fid, findex);
featurevalue = finfo(fid, featurename);
put +2 findex= featurename= featurevalue=;
end;
fid = fclose(fid);
end;
rc = filename(fileref);
end;
did = dclose(did);
end;
rc = filename (dfileref);
run;
After examining the log you can pare down the code needed to gather specific desired information into a data set. You can then use SQL queries to further act upon the data:
data csv_files(keep=fullname lastmod where=(fullname like '%.csv'));
length dfileref fileref $8 folder $200;
folder = 'C:Temp';
rc = filename (dfileref, folder);
did = dopen(dfileref);
if did then do;
do dindex = 1 to dnum(did);
entryname = dread(did,dindex);
rc = filename(fileref, cats(folder, '/', entryname));
fid = fopen (fileref);
if fid then do;
fullname = finfo(fid,'Filename');
lastmod = input(finfo(fid,'Last Modified'), datetime18.); format lastmod datetime18.;
output;
fid = fclose(fid);
end;
rc = filename(fileref);
end;
did = dclose(did);
end;
rc = filename (dfileref);
run;
proc sql;
create table csv_newest as
select *, scan(scan(fullname,-1,'_'),1,'.') as tag
from csv_files
where prxmatch ('/_.+.csv$/', fullname)
having lastmod = max(lastmod)
;
An operating system independent technique would use SAS External File functions such as dopen
, fopen
and finfo
to obtain information about a folder and it's items.
Consider this sample code that does a 'full dump' of available information whilst parsing C:Temp
on a Windows machine:
data _null_;
length dfileref fileref $8 folder $200;
rc = filename (dfileref, 'C:Temp');
did = dopen(dfileref);
if did then do;
do index = 1 to doptnum(did);
featurename = doptname(did,index);
featurevalue = dinfo(did,featurename);
put index= featurename= featurevalue=;
if featurename = 'Directory' then folder = featurevalue;
end;
do dindex = 1 to dnum(did);
entryname = dread(did,dindex);
put dindex= entryname=;
rc = filename(fileref, cats(folder, '/', entryname));
fid = fopen (fileref); * if entry is another folder fid will be 0;
if fid then do;
do findex = 1 to foptnum(fid);
featurename = foptname(fid, findex);
featurevalue = finfo(fid, featurename);
put +2 findex= featurename= featurevalue=;
end;
fid = fclose(fid);
end;
rc = filename(fileref);
end;
did = dclose(did);
end;
rc = filename (dfileref);
run;
After examining the log you can pare down the code needed to gather specific desired information into a data set. You can then use SQL queries to further act upon the data:
data csv_files(keep=fullname lastmod where=(fullname like '%.csv'));
length dfileref fileref $8 folder $200;
folder = 'C:Temp';
rc = filename (dfileref, folder);
did = dopen(dfileref);
if did then do;
do dindex = 1 to dnum(did);
entryname = dread(did,dindex);
rc = filename(fileref, cats(folder, '/', entryname));
fid = fopen (fileref);
if fid then do;
fullname = finfo(fid,'Filename');
lastmod = input(finfo(fid,'Last Modified'), datetime18.); format lastmod datetime18.;
output;
fid = fclose(fid);
end;
rc = filename(fileref);
end;
did = dclose(did);
end;
rc = filename (dfileref);
run;
proc sql;
create table csv_newest as
select *, scan(scan(fullname,-1,'_'),1,'.') as tag
from csv_files
where prxmatch ('/_.+.csv$/', fullname)
having lastmod = max(lastmod)
;
answered Nov 23 at 1:49
Richard
8,12921227
8,12921227
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438167%2fparse-file-name-with-sas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
read in the list of files, find the latest one, import it. Exactly how depends on your OS which you did not specify. communities.sas.com/t5/SAS-Programming/…
– Reeza
Nov 22 at 22:02
Please remember to show what you tried How to Ask
– Reeza
Nov 22 at 22:03