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

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!

-------

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

6 REPLIES 6
TomKari
Onyx | Level 15

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

Jagadishkatam
Amethyst | Level 16

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

Thanks,
Jag
Amarnath7
Fluorite | Level 6

for Task 1, Z. format works to add leading 0s

For task 2, can you please add more details?

woo
Barite | Level 11 woo
Barite | Level 11

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!

Fugue
Quartz | Level 8

Regarding your 2nd question, there are many approaches that could be used. Here is one approach that could be adapted to work for you:

http://support.sas.com/kb/26/009.html

Tom
Super User Tom
Super User

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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 6 replies
  • 1174 views
  • 6 likes
  • 6 in conversation