BookmarkSubscribeRSS Feed
chd1425
Calcite | Level 5

I'm renaming variables names with additional numeric characters at the end of the variable name with below macro. If I call the macro separately for each dataset (e.g. %rnVARBOS(hematofup);
%rnVARBOS(BLDCHEMFUP);) it works fine. 

However when I use call execute with a list of all the datasetnames I get the message that it failed to open the &dsn.VAR, "dataset does not exist". If I look at my work library the dataset is present in the work library. Any suggestions why it does not work when using the call execute? 

 

 

%macro rnVARBOS (dsn);
Proc contents data= work.&dsn out=work.&dsn.LIST;
run;
DATA &dsn.VAR (KEEP=NAME);
SET work.&dsn.LIST;
WHERE COUNTC(NAME,"_")>1
AND name NE "__1_3TRT" AND name NE "__1_4TRT"
AND (index(SUBSTR(STRIP(NAME),length(name)-4,5),"_1")>0 OR
index(SUBSTR(STRIP(NAME),length(name)-4,5),"_2")>0 OR
index(SUBSTR(STRIP(NAME),length(name)-4,5),"_3")>0 OR
index(SUBSTR(STRIP(NAME),length(name)-4,5),"_4")>0 OR
index(SUBSTR(STRIP(NAME),length(name)-4,5),"_5")>0 OR
index(SUBSTR(STRIP(NAME),length(name)-4,5),"_6")>0 OR
index(SUBSTR(STRIP(NAME),length(name)-4,5),"_7")>0 OR
index(SUBSTR(STRIP(NAME),length(name)-4,5),"_8")>0 OR
index(SUBSTR(STRIP(NAME),length(name)-4,5),"_9")>0);
RUN;
* Check if dataset opens and if the number of variables;
%global dset nVARs nobs&dsn;
%let dset=&dsn.VAR ;
%let dsid = %sysfunc(open(&dset));
%if &dsid %then
%do;
%let nobs&dsn =%sysfunc(attrn(&dsid,NOBS));
%let nVARs=%sysfunc(attrn(&dsid,NVARS));
%let rc = %sysfunc(close(&dsid));
%put &dset has &nVARs VARiable(s) and &&nobs&dsn observation(s).;
%end;
%else
%put Open for data set &dset failed - %sysfunc(sysmsg());

* Only execute if no_obs >1;
%IF &&nobs&dsn GE 1 %THEN %DO;
* Create new VARiable name;
DATA VAR2&dsn;
SET &dsn.VAR;
length name_new table $50.;
NAME_new=REVERSE(SUBSTR(REVERSE(NAME),(index(REVERSE(NAME),"_")+1)));
Table="&dsn";
RUN;

* Checking number of variables and creating new var names;

Data _null_ ;
Set VAR2&dsn end=last;
Call symputx('VAR'||left (_n_),name);
Call symputx('VARnew'||left (_n_),name_new);
If last then call symputx('numrows',_n_);
Run;

*VARiabelen in dataset renamen;
DATA work.&dsn;
SET work.&dsn;
%DO i=1 %to &numrows;
RENAME &&VAR&i=&&VARnew&i;
%put dsn=&dsn VAR&i RENAME &&VAR&i to &&VARnew&i ;
%END;
run;
%END;
%mend rnVARBOS;

 

data _null_;
set work.dsnLIST1;
call execute('%rnVARBOS('||dsn_nw||');');
run;

 

1 REPLY 1
Kurt_Bremser
Super User

You do have macro statements in your macro; when the macro is fed to the execution queue, these are resolved immediately, while all procedure and data step code has to wait until the calling data step finishes. This causes a timing mismatch between those two types of code.

To prevent premature execution of the macro statements, use the %NRSTR function:

call execute('%nrstr(%rnVARBOS('||dsn_nw||'));');

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 1 reply
  • 414 views
  • 2 likes
  • 2 in conversation