10-16-2017 09:25 PM
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 -
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?
10-16-2017 09:33 PM
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
10-16-2017 09:43 PM
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?
10-17-2017 03:31 AM
From your link, this is the important section:
10-17-2017 06:03 PM
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?
10-17-2017 06:21 PM
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.
10-17-2017 07:02 PM
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?
10-17-2017 08:10 PM
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.
10-17-2017 08:56 PM
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')
10-17-2017 09:11 PM
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;