BookmarkSubscribeRSS Feed
OscarBoots1
Quartz | Level 8

Hi Forum,

 

I'm trying to understand where to add my parameters for the below macro.

 

I understand that I need to declare variables - Directory paths & filenames etc but I'm not clear where I should with this one.

 

http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n0ctmldxf23ixtn1kqsoh5bsgmg8.htm&docset...

 

%macro drive(dir,ext); 
   %local cnt filrf rc did memcnt name; 
   %let cnt=0;          

   %let filrf=mydir;    
   %let rc=%sysfunc(filename(filrf,&dir)); 
   %let did=%sysfunc(dopen(&filrf));
    %if &did ne 0 %then %do;   
   %let memcnt=%sysfunc(dnum(&did));    

    %do i=1 %to &memcnt;              
                       
      %let name=%qscan(%qsysfunc(dread(&did,&i)),-1,.);   

Example on the fist line -

%macro drive(dir,ext); 

Should I be changing dir for the full directory path , and the file extension for ext?

 

I also don't get how filrf gets it value?

%local cnt filrf rc did memcnt name; 

 Can anyone clarify?

 

Thanks

18 REPLIES 18
Reeza
Super User

You don't modify that section at all. You call the macro. 

 

Here's a base example. 

 

%macro print(dsetin=);

proc print data=&dsetin;
run;

%mend;

%print(destin = sashelp.class);
%print(destin = sashelp.cars);

The two parameters there are:

1. dir -> directory of where the files are stored. 

2. ext -> extension of files of interest, in this case XLSX

OscarBoots1
Quartz | Level 8

Thanks Reeza but i still don't follow.

 

I'm used to VBA where you write a macro & then Call it.

 

But any variables need to be Declared, where are the values declared in the script OR do I need to see the Macros own script so I can see what it's doing?

 

Thanks

Reeza
Super User

SAS doesn't require a declaration of variables. 

The full macro script is there. 

OscarBoots1
Quartz | Level 8

Thanks Reeza I'll have a read.

 

Cheers

Kurt_Bremser
Super User

From your link, this is the important section:

 

Invoke the macro. The first parameter is the directory where the files exist. The second parameter is the extension of the files that you are importing.
%drive(c:\temp,csv)

That's all you have to do. Everything before that is just a step-by-step description how the macro works. It is only of interest if you want to learn writing macros yourself. But before that, you must familiarize yourself with all the Base SAS functions used.

OscarBoots1
Quartz | Level 8

Thanks Kurt,

 

I replaced the 'dir' with my UNC path to a Network Folder & xls as the file type.

 

I'm getting an error as the path is > 32 characters.

 

So this what my code looks like at the start;

 

 %macro drive("\\abc\def\ghi\My_Report","xls"); 
   %local cnt filrf rc did memcnt name; 
   %let cnt=0;       

By the way I've tried Apostrophes & Quotes top wrap the path.

 

Any suggestions how I can replace the path?

 

Do I add a line that says dir = 'Path_name' - for example?

Reeza
Super User

YOU DO NOT CHANGE ANYTHING BETWEEN THE MACRO AND MEND STATEMENTS. 

Unless you're trying to write your own, which I don't think you are. 

 

You use the macro by calling the name.

 

%drive(\\abc\def\ghi\My_Report,xls); 

 

OscarBoots1
Quartz | Level 8

Gotcha, thanks Reeza,

 

So it's similar to what I've used in SQL Server a tvf or svf.

 

Thanks

OscarBoots1
Quartz | Level 8

One more for you, I want the .xls file to be imported into my SAS EG Project as a table.

 

The macro is running but I'm not sure whether I should use apostrophes, Quotes or nothing.

 

& I'm not getting any output?

 

Thanks

Reeza
Super User

That depends on the macro. I can’t see your code so I have no idea of what you ran or what to expect. 

 

If you’re using the code from the appendix that’s only the list of files. The full I linked to is required to import all the files. Or something along those lines. 

OscarBoots1
Quartz | Level 8

Ok, I've posted the full code I'm running, problem is no tables imported into EG?

 

%macro drive(dir,ext); 
   %local cnt filrf rc did memcnt name; 
   %let cnt=0;          

   %let filrf=mydir;    
   %let rc=%sysfunc(filename(filrf,&dir)); 
   %let did=%sysfunc(dopen(&filrf));
    %if &did ne 0 %then %do;   
   %let memcnt=%sysfunc(dnum(&did));    

    %do i=1 %to &memcnt;              
                       
      %let name=%qscan(%qsysfunc(dread(&did,&i)),-1,.);                    
                    
      %if %qupcase(%qsysfunc(dread(&did,&i))) ne %qupcase(&name) %then %do;
       %if %superq(ext) = %superq(name) %then %do;                         
          %let cnt=%eval(&cnt+1);       
          %put %qsysfunc(dread(&did,&i));  
          proc import datafile="&dir\%qsysfunc(dread(&did,&i))" out=dsn&cnt 
           dbms=xls replace;            
          run;          
       %end; 
      %end;  

    %end;
      %end;
  %else %put &dir cannot be open.;
  %let rc=%sysfunc(dclose(&did));      
             
 %mend drive;
 
%drive('F:\Data\Reports\Test_Reports','xls') 

Reeza
Super User

You definitely don't need quotes in the macro call. 

A macro is more like a stored procedure in SQL that you can call with parameters. 

 

Set the following options before the macro call, rerun it and then check your log. You'll see the exact code that's being submitted. If the code doesn't look like valid SAS syntax it can't run.

 

options mprint symbolgen;
OscarBoots1
Quartz | Level 8

Thanks I did that.

 

The code looks OK but I'm getting an  error saying the dir path cannot be opened?

 

 

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!

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