BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
wb85
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
Astounding
PROC Star

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?

ballardw
Super User

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.

mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

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

%put &want;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 2110 views
  • 1 like
  • 5 in conversation