- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content