DATA Step, Macro, Functions and more

Get sub-strings/directory paths from macro string

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Get sub-strings/directory paths from macro string

Hi Friends,

 

I'm using SAS Studio 3.6 and I want to get the a sub directory paths from macro string.

Ex. - I have string &path as below -

 

%let path=&_SASPROGRAMFILE ;

 

%put &path;

C:\myfile\souce\ABC123\XYZ111\code\prg\myprg.sas

 

from above path I want to create macros which should give me subdirectory  paths like -

 

root=C:\myfile\souce\

project=ABC123

Study=XYZ111

 

The folder structures are standard from project to project and study to study. so My aim is to assign libraries autoamtically somthing like this.

 

libname raw "&root.\&project.\&study\data\raw";

 

 

Thanks,

 

- Nikhil


Accepted Solutions
Solution
‎09-28-2017 01:17 AM
Super User
Posts: 7,794

Re: Get sub-strings/directory paths from macro string

Posted in reply to nikhilwagh

Do it in a data step and use call symput:

%let path=C:\myfile\souce\ABC123\XYZ111\code\prg\myprg.sas;

data _null_;
path = "&path";
parts = countw(path,'\');
length root $200 project study $50;
do i = 1 to parts - 5;
  root = catx('\',root,scan(path,i,'\'));
end;
project = scan(path,parts - 4,'\');
study = scan(path,parts - 3,'\');
call symput('root',trim(root));
call symput('project',trim(project));
call symput('study',trim(study));
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎09-28-2017 01:17 AM
Super User
Posts: 7,794

Re: Get sub-strings/directory paths from macro string

Posted in reply to nikhilwagh

Do it in a data step and use call symput:

%let path=C:\myfile\souce\ABC123\XYZ111\code\prg\myprg.sas;

data _null_;
path = "&path";
parts = countw(path,'\');
length root $200 project study $50;
do i = 1 to parts - 5;
  root = catx('\',root,scan(path,i,'\'));
end;
project = scan(path,parts - 4,'\');
study = scan(path,parts - 3,'\');
call symput('root',trim(root));
call symput('project',trim(project));
call symput('study',trim(study));
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 5

Re: Get sub-strings/directory paths from macro string

Posted in reply to KurtBremser

Thank you Kurt!

It served my purpose perfectly.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 96 views
  • 1 like
  • 2 in conversation