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

Hello, I used http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n0ctmldxf23ixtn1kqsoh5bsgmg8.htm&docset... to set up my macro and it will happily chug along through my directory full of csvs.

 

Unfortunately the macro seems to be processing the files in a random order. My csv files overlap in date ranges and I need to make sure they process in order so my code handles overwriting data from older files correctly. 

 

The file names in my directory start with values which increase (but dont increment by any specific amount), with the oldest files having the lowest value as the first six digits:

 

138281_longfilename.csv

142049_longfilename.csv

192820_longfilename.csv

 

Any suggestions on how to tell the macro to sort the files in the directory by the first 6 digits?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Make the list of names as a dataset.  If you are using Windows instead of Unix then adjust the command used to get the list of files appropriately.  If you cannot use PIPE then use a data step that uses the method in your posted link. No need to do it in macro code instead of a data step since your macro is already generating other SAS code.

%let mydir=/folder/where/my/csv/files/live;

data files;
  infile "cd &mydir ; ls *.csv" pipe truncover;
  input filename $256. ;
run;

then sort the list into the order you want to process them.

proc sort data=files;
  by filename;
run;

Then use the list to generate code

data datasets;
  fileno+1;
  set files;
  length memname $32. ;
  memname = cats('csv',fileno);
  call execute(catx(' '
       ,'proc import datafile='
       ,quote(catx('/','&mydir',filename))
       ,'out=',memname,'replace'
       ,'dbms=csv'
       ,';run;'
  ));
run;

 

View solution in original post

5 REPLIES 5
Reeza
Super User
This particular example only imports each data individually, and creates a unique data set for each file. Not sure how that affects anything in order, the SAS data sets will be each unique to the file and you can then process them further in whatever order you like. Add a step that adds the filename to the data for each data set or to the dataset name if you need that information.
clocks212
Calcite | Level 5

I should have added that I replaced this line in the example...

 

proc import datafile="&dir\%qsysfunc(dread(&did,&i))" out=dsn&cnt 
           dbms=csv replace;     

...with the big block of import and de-dup code which I used to process one file at a time. So the "Example 1..." code no longer imports each file as its own data set but instead imports the file, does some proc sql work comparing, deduping, and union'ing it with my master table.

Reeza
Super User
I can only see what you've shown and can only respond accordingly.
Tom
Super User Tom
Super User

Make the list of names as a dataset.  If you are using Windows instead of Unix then adjust the command used to get the list of files appropriately.  If you cannot use PIPE then use a data step that uses the method in your posted link. No need to do it in macro code instead of a data step since your macro is already generating other SAS code.

%let mydir=/folder/where/my/csv/files/live;

data files;
  infile "cd &mydir ; ls *.csv" pipe truncover;
  input filename $256. ;
run;

then sort the list into the order you want to process them.

proc sort data=files;
  by filename;
run;

Then use the list to generate code

data datasets;
  fileno+1;
  set files;
  length memname $32. ;
  memname = cats('csv',fileno);
  call execute(catx(' '
       ,'proc import datafile='
       ,quote(catx('/','&mydir',filename))
       ,'out=',memname,'replace'
       ,'dbms=csv'
       ,';run;'
  ));
run;

 

clocks212
Calcite | Level 5

That is exactly what I need. Thank you Tom!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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