BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
harrylui
Obsidian | Level 7

good day all,

 

can i separate a name by blank?

 

for example 

 

Name

Liverpool  Virgil van Dijk

 

separate a name by blank into 4 columns

 

column1    column2  column3  column4

Liverpool    Virgil       van          Dijk

 

is there any way to do it?

 

thanks in advance

 

Regards,

Harry

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @harrylui 

 

If you want to concatenate several columns using a blank as a delimiter, you can use the CATX function as suggested by @PeterClemmensen 

 

/* Concatenate */

data have;
	input column1:$30. column2:$30. column3:$30. column4:$30.;
	datalines;
Liverpool  Virgil van Dijk
;
run;
 
data want;
	set have;
	name = catx(" ",column1, column2, column3, column4);
run;

If you want to separate the words of a sting in as many columns, you can do this, using the COUNTW() and SCAN() function:

 

/* Separate */

data have;
	input name $50.;
	datalines;
Liverpool  Virgil van Dijk
;
run;

proc sql noprint;
	select max(countw(name)) into:max_count from have;
quit;

data want;
	set have;
	array column(&max_count) $ 30;
	do i=1 to countw(name);
		column(i) = scan(name,i," ");
	end;
	drop i;
run;

Best,

View solution in original post

2 REPLIES 2
ed_sas_member
Meteorite | Level 14

Hi @harrylui 

 

If you want to concatenate several columns using a blank as a delimiter, you can use the CATX function as suggested by @PeterClemmensen 

 

/* Concatenate */

data have;
	input column1:$30. column2:$30. column3:$30. column4:$30.;
	datalines;
Liverpool  Virgil van Dijk
;
run;
 
data want;
	set have;
	name = catx(" ",column1, column2, column3, column4);
run;

If you want to separate the words of a sting in as many columns, you can do this, using the COUNTW() and SCAN() function:

 

/* Separate */

data have;
	input name $50.;
	datalines;
Liverpool  Virgil van Dijk
;
run;

proc sql noprint;
	select max(countw(name)) into:max_count from have;
quit;

data want;
	set have;
	array column(&max_count) $ 30;
	do i=1 to countw(name);
		column(i) = scan(name,i," ");
	end;
	drop i;
run;

Best,

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