BookmarkSubscribeRSS Feed
frank1947
Calcite | Level 5

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.

9 REPLIES 9
Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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?

audioa84
Calcite | Level 5

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.

frank1947
Calcite | Level 5

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

Thanks...

audioa84
Calcite | Level 5

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

frank1947
Calcite | Level 5

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

LinusH
Tourmaline | Level 20

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
frank1947
Calcite | Level 5

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;

learner_sas
Quartz | Level 8

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,

Haikuo
Onyx | Level 15

data have;

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

  output;

  end;

run;

data want;

  set have;

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

run;

Haikuo

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 9 replies
  • 3424 views
  • 2 likes
  • 6 in conversation