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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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