Skip to content

Instantly share code, notes, and snippets.

@rudvfaden
Created October 28, 2021 07:30
Show Gist options
  • Save rudvfaden/b30546cf6412564f24f2fcad1d241531 to your computer and use it in GitHub Desktop.
Save rudvfaden/b30546cf6412564f24f2fcad1d241531 to your computer and use it in GitHub Desktop.
recursivly creates folders in sas like mkdir -p in linux
%macro jbdw_create_folder(dname);
%* laver recurivet alle manglende mapper i en given sti;
%if %symexist(crdir)=0 %then %do;
%let crdir=;
*Erstatter \ med /;
%let dname = %sysfunc(translate(&dname,/,\));
*makes it indifferent wheter or not you add / to the end of the string;
%let dname=%sysfunc(tranwrd(%sysfunc(cats(&dname,/)),//,/));
%end;
%* starter fra toppen og tjekker om mappen finde. Hvis den ikke findes dannes den;
%if %index(&dname,/)>0 %then %do;
%let dir=%sysfunc(substrn(&dname,1,%index(&dname,/)));
%let crdir=&crdir&dir;
%let topdir=%sysfunc(substrn(&crdir,1,%length(&crdir)-%length(%qscan(&crdir,-1,/))-1));
%let dname = %sysfunc(substrn(&dname,%index(&dname,/)+1));
%if not %sysfunc(fileexist("&crdir")) %then %do;
%put NOTE: opretter mappen &crdir;
data _null_;
rc = dcreate(compress("&dir",'/'), "&topdir");
if missing(rc) then do;
mappe = tranwrd("&crdir",'//','/');
put 'ERROR: mappen kan ikke oprettes: ' mappe=;
abort nolist;
end;
run;
%end;
%jbdw_create_folder(&dname);
%end;
%mend jbdw_create_folder;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment