1

I am trying to rename the variables based on one array elements in the folloing way,

%let var= class name gender;

data want; 
set have;

%global noof; 

array point(*)$   %str(&var)  ;
a=dim(point);
call symputx('noof',a);


array newvar(&noof);


do i=1 to &noof;

newvar(i)=translate(point(i),',','.');

end; 

drop &var; 

do i=1 to &noof;

rename newvar(i)=vname(point(i));

end; 

run;

I want to rename the new variables to the first array elemets.

LOG:

 rename newvar(i)=vname(point(i));
                  -
                  22
                  76
ERROR 22-322: Syntax error, expecting one of the following: -, :, =.

ERROR 76-322: Syntax error, statement will be ignored.
4
  • You cannot use functions in a RENAME statement. Before you start using macro variables or macro code to generate SAS code write out the SAS code you want to generate. Show us what SAS code you are trying to generate. Commented Oct 6, 2017 at 12:56
  • I think the title says exactly what he wants - he wants to rename the variables in an array with names from another array. The code inside the data step is superfluous to the question. Commented Oct 6, 2017 at 13:00
  • @DomPazz ya you are right., i think I will change the question. Commented Oct 6, 2017 at 13:01
  • Go vote on my new enhancement request on sascommunities site. communities.sas.com/t5/SASware-Ballot-Ideas/… Commented Oct 6, 2017 at 13:43

1 Answer 1

1

Unfortunately, the value on the RHS of the RENAME statement must be a literal. The statement is evaluated at compile time, not run time.

Try this:

%let var= class name gender;

%macro translate(datain,dataout,vars);
%local n i var;
%let n=%sysfunc(countw(&vars));


data &dataout(rename=(
    %do i=1 %to &n;
        %let var = %scan(&vars,&i);
        newvar&i = &var
    %end;
)); 
set &datain;

array invars(&n) $  &vars  ;

array newvar(&n) $;


do i=1 to &n;
    newvar(i)=translate(invars(i),',','.');
end; 

drop &vars i; 

run;
%mend;

data test;
class = "1,2,3";
name= "Dom,Pazzula";
gender="M";
run;

%translate(test,out,&var);

You can run into issues if the length of these character variables are too large. The new variables might be truncated. You will have to modify this to add a length statement.

Sign up to request clarification or add additional context in comments.

2 Comments

This works, i understand, renaming at the output dataset level.
The syntax in this post works because it generates the full list of old=new pairs of variables to rename. That will work in both a regular RENAME statement or by using the RENAME= dataset option on the output dataset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.