BookmarkSubscribeRSS Feed
JT99
Obsidian | Level 7
Hello! My data looks like this:
Acct col1 col2 col3
ABC 45 375 89756
TFA 609 8320 835
FGT 5907 561 413
Col1-col3 are character type.
My code is:
Data final;set cols;
If length(col1)<= 2 then code='02'
Else if lengh(col1)=3 then code ='03;
Else if length(col1)=4 then code ='04';
Newphone1=cats(code,col1);
Run;
How do i make the code simpler so that i wont have to do a similar data step to col2 and col3?
4 REPLIES 4
Kurt_Bremser
Super User

Something like this?

data have;
input Acct $ (col1-col3) ($);
cards;
ABC 45 375 89756
TFA 609 8320 835
FGT 5907 561 413
;
run;

data want;
set have;
array cols {*} col1-col3;
array newphone {*} $ newphone1-newphone3;
do i = 1 to 3;
  newphone{i} = cats(put(length(cols{i}),z2.),cols{i});
end;
drop i;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Some questions.  Firstly, why are you trying to do this using text numbers?  Numeric data types are there for a reason.  Secondly, what is the reasoning behind this, newphone would end up looking like:

0245

03609

045907

Which doesn't really make sense to me.  What is col1-col3, it looks from the var name newphone, to be a number, but catting on an 02/03 etc. wouldn't make the result a number.  I mean you "could" do:

data have;
  input Acct $ col1 $ col2 $ col3 $;
datalines;
ABC 45 375 89756
TFA 609 8320 835
FGT 5907 561 413
;
run;

data want;
  length newphone $20;
  set have;
  newphone=cats("0",put(lengthn(col1),best.),col1);
run;

But why?

JT99
Obsidian | Level 7
They are phone numbers. I didnt want to write the whole number because I'm typing on my phone. The whole code is longer than what I wrote here.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2041 views
  • 0 likes
  • 4 in conversation