dynamically creating folders

Accepted Solution Solved
Reply
Super Contributor
Posts: 413
Accepted Solution

dynamically creating folders

[ Edited ]

Hi,

 

I have a macro list with the names of 3 comapanies: A,B and C, and the variable for the compnies is "company"


for each company I would like to create a folder on my computer, and I use the following code:

 

data _null_;
do while &company ne ;
x "mkdir c:\test\&company";
end;
run;

 

 

On the other hand when I just do:

x "mkdir c:\test\A";
x "mkdir c:\test\B";
x "mkdir c:\test\C";

I get the 3 folders as wanted, so it seems that there is a problem with my iteration.

 

Thank you!


Accepted Solutions
Solution
‎04-12-2016 12:16 AM
Grand Advisor
Posts: 17,338

Re: dynamically creating folders

*Maybe you can skip the macro variable step, 
assuming you create it from a list of companies in a dataset;

data have; input company $; cards; A B C ; run; data new_dirs; set have; new_dir= dcreate(company, "C:\test\"); run;

View solution in original post


All Replies
Grand Advisor
Posts: 17,338

Re: dynamically creating folders

Your loop is specified incorrectly. 

 

You can use dcreate as well, fyi. 

 

What does your macro variable look like, resolve to? I'm not sure how you expect it to loop. 

Solution
‎04-12-2016 12:16 AM
Grand Advisor
Posts: 17,338

Re: dynamically creating folders

*Maybe you can skip the macro variable step, 
assuming you create it from a list of companies in a dataset;

data have; input company $; cards; A B C ; run; data new_dirs; set have; new_dir= dcreate(company, "C:\test\"); run;
Super Contributor
Posts: 413

Re: dynamically creating folders

[ Edited ]

Hi Reeza,

 

thnka sfor the code, I ran it but the column "new_dir" that it created was empty (and no new folders...).

 

Initially I had a data set "test" with many lines per company, then I extracted the unique comany names witht he following code:

 

proc sql noprint;
create table companies as select distinct company into: cmlst separated by ' ' from test;
quit;

And then I did the my code...

 

Thanks!

Grand Advisor
Posts: 17,338

Re: dynamically creating folders

Post post your code and log. 

 


proc sql noprint;
create table companies as select distinct company from test;
quit;

Data want; 
Set companies; 
New_dir=Dcreate(company, 'C:\test\');
Run;  

Also check your folder to see if they're created. 

 

Super Contributor
Posts: 413

Re: dynamically creating folders

Hi Reeza,

 

I am getting very weired results:

 

Once I tried your sample code when the 3 companies were written in a data step and actually got the 3 folders!!! (and the paths to the directories were included in the new_dir).

 

Then I wanted to do the same thing with my data but didn't get anything (and the new_dir is empty), and when wanted to re do your sample code also didn't get anything! There is no error message whatsoever.

???

 

Thanks!

Grand Advisor
Posts: 17,338

Re: dynamically creating folders

No, there is no,error message. Read the doc regarding the function. 

 

If your having issues you can use call system instead and build file path using CAT functions. 

 

If the folder already exists they won't get overwritten. 

Super Contributor
Posts: 413

Re: dynamically creating folders

Hi Reeza,

 

sorry for being so confusing but now it works all the time!!!

 

Its just that I realized that the directory "test" must already exist in order for the other directories to be put into it.

 

Last time when your sample code worked and I wanted to redo it on my company data I actually deleted the "test" directory (which somehow I had from previous attempts) in order to see how the code fully works, and that's when I wasn't getting anything becasue from what now I understand the function DECREATE must be given a parent directory since it doesn't create the "full path" but rather only the "end of the line" directory.

 

Thank you for your help and for making me discover DECREATE!!! 

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 434 views
  • 3 likes
  • 2 in conversation