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

Hi,

I have to fetch the SAS dataset from XYZ location out of the SAS Server which is having N number of folders and Those folders contains N numbers of sub-folders.Sub-folders contains different extension Files including SAS,therefore I need to fetch Only SAS file from every folders as well sub-folder and need copy to some ABC Location out of the SAS server in One Folder.

Your help will appreciate. Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

So, I have two files in: s:\temp\rob\xyz, ABC.txt and ABC2.txt (it doesn't matter what the file extension is, they could just as easily be .sas or .sas7bdat the process is the same).  Then I have a subdirectory s:\temp\rob\xyz\dgh with two files ERT.txt, ERT2.txt.  I then submit this program in SAS:

filename tmp pipe 'dir "S:\Temp\Rob\XYZ" /s /b';

data dir;
  length buffer $2000.;
  infile tmp;
  input buffer $2000.;
  command="x 'copy "||'"'||strip(buffer)||'" "s:\temp\rob"'||"';";
  call execute(command);
run;

This reads in the directory listing for XYZ with all subdirectories, bare format so only path\file.  I then call execute a system command to copy each one to a new folder s:\temp\rob.  The x 'copy "..." "..."'; is a dos command to copy a file (of any type) to a new location.  I feed it with the directory listing.  You can modify the above to only process SAS programs or datasets with a simple if then else.

View solution in original post

16 REPLIES 16
LinusH
Tourmaline | Level 20

If you are speaking of physical folders, and Base SAS data sets, you need some UNIX/Windows scripting expert to help you fins all locations.

If you are lucky, there exist autoexec.sas-files or similar with libref statements which lets you fins your data locations.

Data never sleeps
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Have a search on here for directory listings, e.g.

filename mypipe 'dir "..."';

data temp;

     infile mypipe...;

run;

Basically you send a DOS directory command output back into a dataset where you can pull out directories/files etc. and then use that further in your processing.

dishant
Calcite | Level 5

Hi,

As per your suggestion its completely new concept for me. Could you please elaborate it with example, how to fetch SAS Codes from one location within sub-folders and copy to another location all codes in one folder so it would be easy for me to understand.

dishant
Calcite | Level 5

Hi RW9,

I was trying to catch your logic Initially got the success but while copying part I got failed. Below was my try.


%global  pt ;

%let pt = 'dir "XYZ" /s';

%put &pt.;

filename DIRLIST1 pipe &pt;

Data dirlist ;

     length buffer $256 ;

     infile dirlist1 length=reclen ;

     input buffer $varying256. reclen ;

Run ;

Data path_file;

  set dirlist;

  where index(strip(upcase(buffer)),'.SAS') > 0 ;*and scan(strip(upcase(buffer)),1,"") ^= "DIRECTORY" ;

  num = 1;

Run;

libname dishant "ABC" ;

proc datasets memtype=(data program);

   copy out=dishant;

      select path_file ;

run;

As per Code In my path_file Dataset I have all SAS File Now want to copy those all files into ABC location Instead of path_file Dataset Contains file. Please Help Me on copying the files. Thanks In advance.

Kurt_Bremser
Super User

In the buffer variable, you have the whole path name of the files you searched for. You now have to extract the directory path (everything up to the last \), which you later use to assign a libname to, so you can use that libname as the source lib in the proc datasets procedure. You also extract the filename itself, strip the .sas7bdat and get the logical SAS member name of the dataset. This is what you use in the select statement of proc datasets.

What it amounts to is a lot of call execute or macro processing.

Or you do it with the help of the operating system.

In UNIX it would look like that:

find $PATH -type f -name \*.sas7bdat -exec cp {} $TARGET \;

I'd assume that windows has a usable equivalent of the find command by now.

Kurt_Bremser
Super User

A word of caution: memtype=program does not cover your ordinary .sas files, which are basically plain text, but precompiled SAS programs!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

So, assuming you have buffer that looks something like:

Buffer

s:\temp\somedirectory\Aprog.sas

s:\temp\somedirectory\Bprog.sas

s:\temp\somedirectory\subdir\data\abc.sas7bdat

Then you could just generate the code from there - assumes the directories to copy to exist!:

data dirlist;

     attrib commandstr format=$2000.;

     if index(buffer,"data")>0 then commandstr='copy "'||strip(buffer)||'" "s:\abc\data"';

     else commandstr='copy "'||strip(buffer)||'" "s:\abc\"';

     call execute('x commandstr');

run;

/* Note - untested, but should be similar */

dishant
Calcite | Level 5

Hi all,

In my Buffer my data will look like below,

Buffer

08/05/2012  06:35           214,016 tblsystem.sas7bdat

06/05/2014  08:03             4,987 DER_CAFQ056A2212_188669.sas

06/05/2014  06:35            13,312 tblcountryregions.sas7bdat

Above Bold characters is my SAS Code in XYZ location those Code Names Showing in Buffer path_file dataset , want to copy all code which is in Buffer Variable to ABC location With Contains Of that code respective.

Please revert back me on this.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

So this code:

filename tmp pipe 'dir "s:\temp\rob" /s';

data a;
  length buffer $2000;
  infile tmp;
  input buffer $2000.;
run;

Will yield something like:

buffer

Volume Serial Number is xxxxx               /* Drop this */

Directory of s:\temp\temp                        /* Drop the Directory of, using tranwrd, and retain this as path-to */

15/02/2014  16:34  <DIR> ...                    /* Drop this */

13/02/2014   12:34  adataset.sas7bdat               /* substr(scan(xx,2," ")+1) = filename.

...

You will then have:

pathto                     file

s:\temp\temp          adataset.sas7bdat

And its just a matter of copy "pathto\file" "s:abc"

dishant
Calcite | Level 5

RW9,

matter of copy "pathto\file" "s:abc"only giving the problem,Can you please give example for Better.


My Final data Is Like Below.


Filename                                        FileLoc

tblcountryregions.sas7bdat     \\s:\abc\ISCR

tblsystem.sas7bdat               \\s:\abc\ISCR\181395\A

HAA00616_DER.sas              \\s:\abc\ISCR\181396


As per My criteria I want All File In "\\T:\xyz" Location.How?

dishant
Calcite | Level 5

RW9,

As Above message I Need all File In "\\T:\xyz" Location Which look Like Below,

tblcountryregions.sas7bdat

tblsystem.sas7bdat

tblsystem.sas7bdat



On Clicking of respective file it should contains Respective material for respective files. How?

dishant
Calcite | Level 5

RW9,

Its Containing N Number Of Files At N number of folders and sub-folders,want to fetch those Only SAS File and need to Copy "\\T:\xyz" location,Therefore In my Filename Have Only SAS File With Respective FileLoc and finally need to copy all those file to above location.How?

Please revert me back on this to achieve final step of destination.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

So, I have two files in: s:\temp\rob\xyz, ABC.txt and ABC2.txt (it doesn't matter what the file extension is, they could just as easily be .sas or .sas7bdat the process is the same).  Then I have a subdirectory s:\temp\rob\xyz\dgh with two files ERT.txt, ERT2.txt.  I then submit this program in SAS:

filename tmp pipe 'dir "S:\Temp\Rob\XYZ" /s /b';

data dir;
  length buffer $2000.;
  infile tmp;
  input buffer $2000.;
  command="x 'copy "||'"'||strip(buffer)||'" "s:\temp\rob"'||"';";
  call execute(command);
run;

This reads in the directory listing for XYZ with all subdirectories, bare format so only path\file.  I then call execute a system command to copy each one to a new folder s:\temp\rob.  The x 'copy "..." "..."'; is a dos command to copy a file (of any type) to a new location.  I feed it with the directory listing.  You can modify the above to only process SAS programs or datasets with a simple if then else.

dishant
Calcite | Level 5

RW9,

Sorry for more update but last to want fetch only .sas File not .sas7bdat, since as per your code its fetching both files.How to avoid that?

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 16 replies
  • 2301 views
  • 6 likes
  • 5 in conversation