DATA Step, Macro, Functions and more

using macro to create libname

Accepted Solution Solved
Reply
Contributor
Posts: 71
Accepted Solution

using macro to create libname

Hi,

In the below code I am using a macro to create a folder if doesn't exist and use the same folder to assign a libname. When i run the code  for the first time the folder create step works fine but the libname is not getting assigned.

However, if i run the same code again in the same session the libname is getting successfully assigned.

Options noxwait;

%Let monyyyy = %sysfunc(putn(%sysfunc(today()),monyy7.));

%Macro chk;

Data _null_;

if not fileexist("C:\Reports\&monyyyy.\&sysdate.") then rc=system("mkdir "C:\Reports\&monyyyy.\&sysdate.");

Run;

%Let fpath = C:\Reports\&monyyyy.\&sysdate.;

libname rep "&fpath.";

%Mend;

%Chk;

Does anybody have any idea, why is this happening.? Any help would be appreciated.


Accepted Solutions
Solution
‎06-08-2012 11:30 AM
Super User
Posts: 11,343

Re: using macro to create libname

I would try removing the unneeded periods after &sysdate and &fpath. The example code then works on my system.

View solution in original post


All Replies
Respected Advisor
Posts: 4,925

Re: using macro to create libname

I dont think you need a macro at all. But the error could be caused by missing quotes in your system function call.

PG

PG
Contributor
Posts: 71

Re: using macro to create libname

PG,

Thanks for the reply. I didn't list the entire code  here and i have some steps after the libname which requires Macro. Actually, there was no quotes in my original code I typed it incorrectly in the forum, good catch though. I removed the quotes in the below code. Please let me know if you think something else could be causing the error.

%Let monyyyy = %sysfunc(putn(%sysfunc(today()),monyy7.));

%Macro chk;

Data _null_;

if not fileexist("C:\Reports\&monyyyy.\&sysdate.") then rc=system("mkdir C:\Reports\&monyyyy.\&sysdate.");

Run;

%Let fpath = C:\Reports\&monyyyy.\&sysdate.;

libname rep "&fpath.";

%Mend;

%Chk;

Solution
‎06-08-2012 11:30 AM
Super User
Posts: 11,343

Re: using macro to create libname

I would try removing the unneeded periods after &sysdate and &fpath. The example code then works on my system.

Contributor
Posts: 71

Re: using macro to create libname

Removing the periods worked. Thank you very much!!!

Super User
Super User
Posts: 7,059

Re: using macro to create libname

The trailing periods looked fine before, perhaps you removed some spaces when you removed the periods?  Using proportional fonts can make that type of thing very hard to see.

It would be easier and less error prone to assign the FPATH macro variable and then use it.  Then you have the logic for building the path in one place instead of three.

You also might want to quote the path when passing it to the mkdir command. Many DOS commands require quotes to handle paths with embedded spaces correctly.

%Let monyyyy = %sysfunc(putn(%sysfunc(today()),monyy7.));

%macro chk;

%Let fpath = C:\Reports\&monyyyy\&sysdate;

data _null_;

  if not fileexist("&fpath") then rc=system("mkdir ""&fpath""");

run;

libname rep "&fpath";

%mend chk;


%chk;

Contributor
Posts: 71

Re: using macro to create libname

Tom,

Your solution make sense and it worked. Thanks for the reply!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 9803 views
  • 5 likes
  • 4 in conversation