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.
%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
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
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
SAS doesn't require a declaration of variables.
The full macro script is there.
Here's a good tutorial on macros:
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/
Thanks Reeza I'll have a read.
Cheers
From your link, this is the important section:
%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.
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?
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);
Gotcha, thanks Reeza,
So it's similar to what I've used in SQL Server a tvf or svf.
Thanks
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
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.
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')
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;
Thanks I did that.
The code looks OK but I'm getting an error saying the dir path cannot be opened?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: