Desktop productivity for business analysts and programmers

adding zero '0' in front of variable 'telno'

Reply
Occasional Contributor
Posts: 9

adding zero '0' in front of variable 'telno'

Hi Guys, i have two variable in dataset named 'telno' and 'state', couple of state required zero '0' in front of 'telno' variable. if i wants to add zero '0' for couple of states only, how can i do that?

please help!

Thanks.

Super Contributor
Super Contributor
Posts: 435

Re: adding zero '0' in front of variable 'telno'

if state in ('NJ','NY'....)  then phone=' 0-' || phone;

Sorry i am new to SAS but this is what i'd think of first

I bet phone is  a char variable and you specified the length at the top of your code?

Occasional Contributor
Posts: 9

Re: adding zero '0' in front of variable 'telno'

Frank, can you confirm if 'telno' is a char (text value)? If it is, Tal has the right idea. You could do an update using PROC SQL by doing the following:


Open a new SAS Program by right clicking the blank area on a process flow,  select new, select Program. Then enter the following code:

PROC SQL;

UPDATE WORK.DATASET

SET telno = '0' || telno

WHERE state IN ('state1','state2');

QUIT;


Just change WORK.DATASET to the name of your data set and change the states and you should be able to run the code. There are other ways to do this as well, I just chose the first one to come to mind.

Occasional Contributor
Posts: 9

Re: adding zero '0' in front of variable 'telno'

Hi audioa84, i think concatenation (||) requires character operands and zcta is a numeric variable so it could updated with a numeric expression only...

Thanks...

Occasional Contributor
Posts: 9

Re: adding zero '0' in front of variable 'telno'

No problem, but I think you replied on the wrong thread. There is no mention of zcta on this thread.

Occasional Contributor
Posts: 9

Re: adding zero '0' in front of variable 'telno'

Ya, i am sorry was working with two datasets together. but 'telno' is numeric so we can apply same logic...Thanks

Esteemed Advisor
Posts: 5,198

Re: adding zero '0' in front of variable 'telno'

If telno is numeric, you can't store the telno with leading 0, that's how SAS (and most other SW) does.

If you like, you could apply the Zn. format to your column, but you can't do that dependent on values in other columns.

So I guess you need to create a new telno char column to fulfill your requirement.

Data never sleeps
Occasional Contributor
Posts: 9

Re: adding zero '0' in front of variable 'telno'

hi guys this onw worked

proc sql;

create table want as

select

     state,

     telno,

     cats(case when state in ('s1','s2','s3') then '0' end, telno) as telnoChr

from

     have;

quit;

Frequent Contributor
Posts: 89

Re: adding zero '0' in front of variable 'telno'

Hello Guys,

I have a character variable which has values like (9945,99,0954) and I want to make variable values like (9945,099,0954).

I want to insert a leading zero if my character variable is of length two.

I am wondering if you can help.

Thanks,

Respected Advisor
Posts: 3,124

Re: adding zero '0' in front of variable 'telno'

data have;

  do var='9945','99','0954';

  output;

  end;

run;

data want;

  set have;

  var=ifc(length(var)=2,'0'||var,var);

run;

Haikuo

Ask a Question
Discussion stats
  • 9 replies
  • 1837 views
  • 2 likes
  • 6 in conversation