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.
I would try removing the unneeded periods after &sysdate and &fpath. The example code then works on my system.
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,
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;
I would try removing the unneeded periods after &sysdate and &fpath. The example code then works on my system.
Removing the periods worked. Thank you very much!!!
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;
Tom,
Your solution make sense and it worked. Thanks for the reply!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.