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

Hi Dear all, I have a question, I want to  create a new list of variables x_a, y_a, and z_a, based on the values of variables x, y, and z.  How can we achieve this in array in batch? because rather than for 3 variables, I want to do this for 20 variables. I'm just giving a simplified example here.

 

array ZERO(*)

 

x

y

z

;

 

do i=1 to dim(ZERO);

if ZERO(i)=0 then [some function that creates a new variable by adding _a to A(i)]=.A;

end;

 

can anyone fill in the brackets [some function that creates a new variable name by adding _a to the variable name that corresponds to ZERO(i)]?

 

Thanks a lot!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Perhaps the current replies are barking up the wrong tree.  Your question doesn't really ask to rename variables.  It talks about creating a new variable based on the original variable.  While macros can come up with the new names, it's easier to type them out:

 

array old {20} x y z .......;

array new {20} x_a y_a z_a .........;

do k=1 to dim(old);

     if old{k}=0 then new{k} = .a;

end;

 

If you were going to attempt this the hard way, these are the pieces that you would need in your macro:

 

a parameter to indicate the suffix:  _a

a parameter to list the original names:  x y z .........

use the list of the original names within the macro to populate one array:

array old {*} &original_names;

logic to populate the new array with a list of names that includes the suffix, based on both incoming parameters

 

Like @Reeza said, it's much easier to just type in the names in the ARRAY statement.  If you need to apply this process multiple times with many different sets of variable names, I might reconsider and recommend the macro approach.

View solution in original post

5 REPLIES 5
Reeza
Super User
There isn't some function that does this that easily. If you want to rename it's easier to write the rename statements...for 20 that's not too bad. If you ahve sequences that are numeric this is super easy:

Rename name1-name3 = new_name1-new_name3;

However, suffixes are more work. You can use an array though if you watned.

array new(*) var1_a var2_a var3_a ....var20_a;*you'll have to list them all;

if zero(i) = 0 then new(i) = .A;

r_behata
Barite | Level 11

@changxuosu : Do you want to create new columns with no values populated (blank )  and the name of the new columns should be based on the values of your existing dataset ? How many observations does your input dataset has ?

Reeza
Super User

Or this method:

 

options mprint;
proc sql noprint;
select catx(' = ', name, catt(name, '_a')) 
into :rename_list separated by " "
from sashelp.vcolumn
where libname='SASHELP' and memname='CLASS';
quit;

data class;
set sashelp.class;
rename &rename_list.;
run;

proc contents data=class;
run;
Astounding
PROC Star

Perhaps the current replies are barking up the wrong tree.  Your question doesn't really ask to rename variables.  It talks about creating a new variable based on the original variable.  While macros can come up with the new names, it's easier to type them out:

 

array old {20} x y z .......;

array new {20} x_a y_a z_a .........;

do k=1 to dim(old);

     if old{k}=0 then new{k} = .a;

end;

 

If you were going to attempt this the hard way, these are the pieces that you would need in your macro:

 

a parameter to indicate the suffix:  _a

a parameter to list the original names:  x y z .........

use the list of the original names within the macro to populate one array:

array old {*} &original_names;

logic to populate the new array with a list of names that includes the suffix, based on both incoming parameters

 

Like @Reeza said, it's much easier to just type in the names in the ARRAY statement.  If you need to apply this process multiple times with many different sets of variable names, I might reconsider and recommend the macro approach.

changxuosu
Quartz | Level 8
Thank you Astounding, that's exactly what I'm looking for!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 7901 views
  • 8 likes
  • 4 in conversation