hi guys, i need some help on modifying value for the variable and changing variable name
I have variable 'store_id' and it has 3000 observations. values are 6 digit numerical only and i want to add 0 (zero)
infront of the all value, how can i do that?
this is what i have
store_id
123456
780123
123560
010203
...........
and this is what i want
store_id
0123456
0780123
0123560
0010203
task-2
--------
i have variable name 'customer_name' variable and i want to change it with 'customer_id', i know i can use rename statement but what if i want to use macro like customer_name=&customer_id. ----------how can i use macro...?
please advise - Thanks!
-------
Let's assume that you have a list of names and you want to get the list of OLD=NEW pairs.
%macro rename(list,prefix);
%local i name ;
%do i=1 %to %sysfunc(countw(&list)) ;
%let name = %scan(&list,&i) ;
&name = &prefix.&name
%end;
%mend rename;
Then you could use this macro inside of a rename statement. In a datastep for example:
data want ;
set have ;
rename %rename(id store_id store_add, std_) ;
run;
1. I assume the variable is 6 byte character. You will need to create a new variable to make it 7 character. Here's an example:
data have;
length store_id $6;
input store_id;
cards;
123456
780123
123560
010203
run;
data want;
set have(rename=(store_id=in_store_id));
length store_id $7;
store_id = '0' || in_store_id;
run;
2. This is a little unclear...can you expand?
Tom
For Task1, you can simply use the format z7., this will place a 0 before the values. Please check the code below
data have;
input store_id;
format store_id z7.;
cards;
123456
780123
123560
010203
;
run;
proc print;
run;
for task2, please check the below code where i renamed store_id to id
%macro rename(new);
data want(rename=(store_id=&new));
set have;
run;
%mend;
%rename(id);
Thanks,
Jagadish
Thanks,
Jagadish
for Task 1, Z. format works to add leading 0s
For task 2, can you please add more details?
I got answer for first task, i like both approch...Thanks a LOT!
task 2
--------
i have variable name 'customer_name' variable and i want to change it with 'customer_id', i know i can use rename statement but what if i want to use macro like customer_name=&customer_id. ----------how can i use macro...?
basically i have so many variable name and we are planning to modify each and every variables to something else, lets say standard business variable name, so,
i have variable like below
id
store_id
store_add
customer_name
customer_add
and i want
std_id
std_store_id
std_store_id
std_customer_name
std_customer_add
what is the best way to achive this insted of using rename statement, i want to use macro somehow...is that something looks feasible or still i am asking something inappropriate....
Thanks!
Regarding your 2nd question, there are many approaches that could be used. Here is one approach that could be adapted to work for you:
Let's assume that you have a list of names and you want to get the list of OLD=NEW pairs.
%macro rename(list,prefix);
%local i name ;
%do i=1 %to %sysfunc(countw(&list)) ;
%let name = %scan(&list,&i) ;
&name = &prefix.&name
%end;
%mend rename;
Then you could use this macro inside of a rename statement. In a datastep for example:
data want ;
set have ;
rename %rename(id store_id store_add, std_) ;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.