Desktop productivity for business analysts and programmers

modifying variables

Accepted Solution Solved
Reply
Super Contributor
Super Contributor
Posts: 318
Accepted Solution

modifying variables

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!

-------


Accepted Solutions
Solution
‎09-20-2013 04:45 PM
Super User
Super User
Posts: 6,330

Re: modifying variables

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


All Replies
Trusted Advisor
Posts: 1,056

Re: modifying variables

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

Super User
Posts: 1,111

Re: modifying variables

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
Occasional Contributor
Posts: 8

Re: modifying variables

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

For task 2, can you please add more details?

Super Contributor
Super Contributor
Posts: 318

Re: modifying variables

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!

Super Contributor
Posts: 307

Re: modifying variables

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

Solution
‎09-20-2013 04:45 PM
Super User
Super User
Posts: 6,330

Re: modifying variables

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 345 views
  • 6 likes
  • 6 in conversation