BookmarkSubscribeRSS Feed
RAVI2000
Lapis Lazuli | Level 10
%macro CheckandCreateDir(dir);
   options noxwait;
   %local rc fileref ;
   %let rc = %sysfunc(filename(fileref,&dir)) ;   
   %if %sysfunc(fexist(&fileref)) %then
      %put The directory "&dir" already exists ;
   %else
     %do ;
         %sysexec mkdir "&dir" ;
         %if &sysrc eq 0 %then %put The directory &dir has been created;

Can anyone briefly explain what's going on.

I understood its creating a directory and checking whether it exits with IF ELSE statements.

but am not sure what exactly NOXWAIT option and in the very last IF statement &sysrc is doing ?macrodir.PNG

9 REPLIES 9
RAVI2000
Lapis Lazuli | Level 10
Can anyone briefly explain what's going on. I understood its creating a directory and checking whether it exits with IF ELSE statements. but am not sure what exactly NOXWAIT option and in the very last IF statement &sysrc is doing ?
RAVI2000
Lapis Lazuli | Level 10
What is the difference ? what exactly is it doing in the above program ?
https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=lefunctionsref&docsetTarg...
Kurt_Bremser
Super User

The documentation you linked is about the SYSRC() data step function, not the automatic macro variable, but both do in essence the same thing: report the return code of certain interactions with the operating system.

In your code, the %IF where &SYSRC is used checks if the external call to mkdir succeeded. Today you would use the DCREATE() data step function instead.

Reeza
Super User

This is an old macro that is NO longer required in SAS. You can use either DCREATE() or the option on the libname statement to create the folder. 

 

SAS is an old language so it's easy to find old stuff - 30 years old sometimes around. 

 

I've commend the code below but it's only partial code that you've posted. 

 

 

%macro CheckandCreateDir(dir);
   *if you don't have this option a dos window will pop up and you have to exit it manually;
   options noxwait;
   *creates a local macro variable;
   %local rc fileref ;
   *captures the Return Code (RC) from creating a filename reference;
   *check the documentation to see what the differnet return codes mean;
   %let rc = %sysfunc(filename(fileref,&dir)) ;   
   *checks if it already exists;
   %if %sysfunc(fexist(&fileref)) %then
      %put The directory "&dir" already exists ;
   %else
     %do ;
    *if it does not exist it makes the directory using a mkdir command;
         %sysexec mkdir "&dir" ;
         %if &sysrc eq 0 %then %put The directory &dir has been created;

 

ballardw
Super User

One of the nicer thing about the old SAS code you find, especially related to file manipulation or data step, is that most of it still works.

 

I am glad that I don't have to "forget" the coding I learned for SAS in the 1980's. Unlike when I worked in a shop that used SPSS where every annual update cycle seemed to require redesigning code and rewriting because one or more of the key elements our shop used was removed or the syntax drastically changed.

Reeza
Super User
Most definitely! Backwards compatibility helps you keep customers for sure.
But replace 30 lines of code for something when there's a one liner option in the 9.4 version is also nice. STACKODS from PROC MEANS is one of those options as are things like INDSNAME and colon operator.
Kurt_Bremser
Super User

When things are built rather well from the start, there's no need for up-ending everything.

All books I ever bought about things Microsoft have long gone into the recycling bin, but Ken Thompson's The UNIX Operating System and the classic Kernighan-Richie are still useful parts of my bookshelf.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

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
  • 9 replies
  • 1080 views
  • 2 likes
  • 4 in conversation