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

Re: Need alternative of "Filename" statement with Pipe operator for SAS University Edition and Re: Bulk Download all my files from SAS Ondemand introduce the way to read filenames recursively via modify, replace, and dread without pipe.

data i;
    length i j $800;
    i='!USERPROFILE\Desktop';
run;

data i;
    modify i;
    a=filename("i",catx("\",i,j));
    a=dopen("i");
    if a then do;
        i=catx("\",i,j);
        j="";
    end;
    replace;
    if a;
    do k=1 to dnum(a);
        j=dread(a,k);
        output;
    end;
    a=dclose(a);
    a=filename("i");
run;

Usually, I use neither modify nor replace, but why can the code above read filenames recursively while the code below cannot?

data i;
    length i j $800;
    i='!USERPROFILE\Desktop';
    a=filename("i",catx("\",i,j));
    a=dopen("i");
    if a then do;
        i=catx("\",i,j);
        j="";
    end;
    if a;
    do k=1 to dnum(a);
        j=dread(a,k);
        output;
    end;
    a=dclose(a);
    a=filename("i");
run;

I wonder whether the two separate data steps are necessary.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

It is not really "recursive" in the classical meaning of that term.  What is is doing to appending records to the end of the dataset.

 

A normal data step will stop when it reads past the end of the input dataset (or input file when using INPUT statement).  In this step since you are modifying the input dataset instead of writing to a new dataset the OUTPUT statements are causing extra observations to added to the input dataset.  So those observations written will be read back in later in the data step and when they are directories it will lead to more observations being added.

View solution in original post

2 REPLIES 2
Peter_C
Rhodochrosite | Level 12
probably you need to control the data step loop /iteration, more. With MODIFYstatement, the rules are different from the DATA step without. The second version (one data step) will stop an the end of the first iteration.
Tom
Super User Tom
Super User

It is not really "recursive" in the classical meaning of that term.  What is is doing to appending records to the end of the dataset.

 

A normal data step will stop when it reads past the end of the input dataset (or input file when using INPUT statement).  In this step since you are modifying the input dataset instead of writing to a new dataset the OUTPUT statements are causing extra observations to added to the input dataset.  So those observations written will be read back in later in the data step and when they are directories it will lead to more observations being added.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 917 views
  • 0 likes
  • 3 in conversation