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.

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!

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.

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
  • 4 replies
  • 1240 views
  • 0 likes
  • 4 in conversation