Ghanshyam Savaliya
Ghanshyam Savaliya

Reputation: 618

Define Library in SAS where some of the Folder name will be change each time(When this SAS macro run from different users)

Topic: SAS Library

Difficulty: path(File name and Location) is changing after each run but only some of the details are changing but not the full path (as given in below example). we have also highlighted those field in Bold

I want to write only one code where I can cover all kind of change which happening in file name and location, is it possible?

%let path='C:\Data\variationstring\empcat**A**\person**34**_**1212**\persondata_empcatA_**34**';    
libname test "&path";
proc import datafile="&path\Accounts_**34**.xls"
    out=mydata
    sheet="thefile";
    getnames=no;
run;

When another user run that program then above path will be changed:

%let path='C:\Data\variationstring\empcat**A**\person**49**_**1684**\persondata_empcatA_**49**';    
libname test "&path";
proc import datafile="&path\Accouns_**49**.xls"
    out=mydata
    sheet="thefile";
    getnames=no;
run;

Can anyone help me for this, please?

Thanks

Upvotes: 0

Views: 182

Answers (2)

Joe
Joe

Reputation: 63434

In addition to Grigory's excellent suggestion, it looks to me like you're doing something that would be well served utilizing a data-driven programming approach.

If you have, say, an excel spreadsheet with all of your personnel records - let's say the first number is store and the second is employeeID - and you want to run one report per employeeID, then you write the macro like Grigory suggested; but you call the report from the first dataset.

So here:

proc import file="C:\Data\employeeID.xlsx" 
            out=employees dbms=xlsx 
            replace;
run;

%macro get_employee(store=, employeeID=, empCat=);
%let path="C:\Data\variationstring\empcat&empcat.\person&store._&employeeID.\persondata_empcat&empcat._&store.";    
libname test "&path";
proc import datafile="&path\Accouns_&store..xls"
    out=mydata
    sheet="thefile";
    getnames=no;
run;
%mend get_employee;

proc sql;  *this generates macro calls, look at output to see what the macro variable contains;
  select cats('%get_employee(employeeid=',employeeID,',store=',store,',empcat=',empcat,')') 
  into :get_emp_list separated by ' '
  from employees;
quit;

&get_emp_list.;  *This actually runs all those macro calls;

You can read my paper, Writing Code With Your Data for more details, or find other similar papers online.

Upvotes: 1

Grigory P
Grigory P

Reputation: 191

Try to put it in a macro like this:

%macro import (macro_var1,macro_var2,macro_var3);
%let path="C:\Data\variationstring\empcat**A**\person**&macro_var1.**_**&macro_var2.**\persondata_empcatA_**&macro_var3.**";    
libname test "&path";
proc import datafile="&path\Accouns_**49**.xls"
    out=mydata
    sheet="thefile";
    getnames=no;
run; 
%mend;
%import (34, 1212, 34);
%imoprt (49, 1684, 49);
etc.

when defining path, don't forget to put it in double quotes (") insted of single (')

Upvotes: 1

Related Questions