Parse file name with SAS












-1














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?










share|improve this question






















  • 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
















-1














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?










share|improve this question






















  • 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














-1












-1








-1







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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 21:30









Victor

6,67149158318




6,67149158318












  • 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










  • 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












1 Answer
1






active

oldest

votes


















1














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)
;





share|improve this answer





















    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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)
    ;





    share|improve this answer


























      1














      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)
      ;





      share|improve this answer
























        1












        1








        1






        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)
        ;





        share|improve this answer












        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)
        ;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 at 1:49









        Richard

        8,08921227




        8,08921227






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks