BookmarkSubscribeRSS Feed
JT99
Obsidian | Level 7
Hello everyone!
I have a data with a row of phone numbers like this:

Name number
id1 65747 736356 837364 837336
id2 556773 6738373 8373839 677838
.
.
.
I want number column to have length equal to 4.
Name number
id1 6574
Id1 7736
Id1 3568
And so on

Any help will be appreciated.
5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Something like:

data have;
  id=1; number="65747 736356 837364 837336"; output;
  id=2; number="556773 6738373 8373839 677838"; output;
run;

data want (drop=i number);
  set have;
  number=compress(number);
  length want $4.;
  do i=1 to lengthn(number);
    want=cats(want,char(number,i));
    if mod(i,4)=0 then do;
      output;
      want="";
    end;
  end;
run;
PeterClemmensen
Tourmaline | Level 20

something like this.

 

data have;
input Name$1-3 number$5-50;
datalines;
id1 65747 736356 837364 837336
id2 556773 6738373 8373839 677838
;

data want(keep=name num);
	set have;
	comp_number=compress(number);
	nchar=length(comp_number);
	do i=1 to nchar by 4;
		num=substr(comp_number, i, 4);
		output;
	end;
run;
JT99
Obsidian | Level 7
Thank you both for the help!
Is there a way to not delete spaces?
There is a space between the numbers and I want the space to be retained.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

This is different to what you asked for in the first post.  Also, you should really show this as wanted output, i.e. what spaces are to be used.  As such you maybe able to modify @PeterClemmensen's code slightly to:

data have;
id=1; number="65747 736356 837364 837336"; output;
id=2; number="556773 6738373 8373839 677838"; output;
run;

data want (drop=i number);
set have;
length want $4.;
do i=1 to length(number) by 4;
want=substr(number,i,4);
output;
end;
run;

 

PeterClemmensen
Tourmaline | Level 20

@JT99, there doesn't seem to bee spaces in the desired output data you post? Always a good idea to be clear about what your desired data looks like.

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
  • 5 replies
  • 1034 views
  • 2 likes
  • 3 in conversation