DATA Step, Macro, Functions and more

Create new variables with the same suffix

Accepted Solution Solved
Reply
Occasional Learner
Posts: 1
Accepted Solution

Create new variables with the same suffix

Hello,

 

I'm trying to iterate over a list of variables in a data step, and for each one I want to create a new variable that is identical to the original but has the suffix "_new". Is this possible to do without resorting to using a macro (i.e. just in a data step)?

 

So, what I'm trying to achieve is something along the lines of (I know the below syntax is wrong):

%let varlist = varA varB varC ... varZ;

 

for each var in &varlist do;

                var_new = var;

end;

 

Giving me a new set of varaibles varA_new etc. taat are identical to the originals. I've worked out a way to do this using %scan(&varlist, &i) in a macro, but this is a small bit of a much larger data step and it's not easy to create new macros in the project I'm working on.


Accepted Solutions
Solution
‎10-05-2017 06:12 AM
Super User
Posts: 5,518

Re: Create new variables with the same suffix

SAS doesn't support that.  It does support a few cases where the intent is to rename, rather than create new variables.  So you will need a new approach, possibly this.  Copy the entire data set to a new data set without change.  Then make your calculations based on the new data set, using the original variable names.  If necessary, find a way to combine the results of your calculations with the original data set.

 

By the way, how did you want to handle this if your original data set contains variable names that are 32 characters long?

View solution in original post


All Replies
Solution
‎10-05-2017 06:12 AM
Super User
Posts: 5,518

Re: Create new variables with the same suffix

SAS doesn't support that.  It does support a few cases where the intent is to rename, rather than create new variables.  So you will need a new approach, possibly this.  Copy the entire data set to a new data set without change.  Then make your calculations based on the new data set, using the original variable names.  If necessary, find a way to combine the results of your calculations with the original data set.

 

By the way, how did you want to handle this if your original data set contains variable names that are 32 characters long?

Super User
Posts: 11,343

Re: Create new variables with the same suffix

You may want to consider making a common Prefix if you think you are going to want to do the same things or use them in a list.

If I variables abc cde and dfe and I create new varirable with a common suffix such as _pdq so that I have abc_pdq cde_pdq and dfe_pdq every time I want to address all of those variables I likely have to use the full variable name every time. However if I create variables

pdq_abc pdq_cde and pdq_dfe I can use a variable list such as pdq_:   , the colon being one of the SAS list builders, and that will reference all varaibles that beging with pdq_ .

 

I don't understand the comment about: it's not easy to create new macros in the project 

 

If you can add to the code you place a macro definition before it is needed then use as needed. Or place the macro in an autocall library and don't worry about including it in your code.

Trusted Advisor
Posts: 1,022

Re: Create new variables with the same suffix

You could do this with the %include statement - not exactly macro, but not exactly regular sas statement either.  This program produces these statments, the second of which is generated by the %INCLUDEd code:

 

data new;

  set sashelp.class;

  if 0 then set sashelp.class (rename=(Name=Name_new Sex=Sex_new

                 Age=Age_new Height=Height_new Weight=Weight_new));

run;

 

Here's how, using the VNEXT function:

 

filename tmp temp ;

data _null_;
  set sashelp.class (obs=1);

  length vnam $32 ;
  file tmp;

  put 'if 0 then set sashelp.class (rename=(';
  do I=1 to 6;
    call vnext(vnam);
    if vnam='vnam' then leave;
    put vnam +(-1) '=' vnam +(-1) '_new';
  end;
  put '));';
run;

data new;
  set sashelp.class;
  %include tmp /source2;
run;
Super User
Posts: 10,046

Re: Create new variables with the same suffix

%let varlist = varA varB varC ;
%let want=%sysfunc(prxchange(s/(\w+)/$1_new=$1/,-1,&varlist));

%put &want;
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 174 views
  • 1 like
  • 5 in conversation