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

Hi Everyone,

 

I have a long text variable and I want to split this variables in multiple variables (v1 to Vn) with equal number of character in each variables. Of course, the last variable should has the left-over.

 

In the example below, can you please help me to create variables with (10 letter and space) in each?

 

Thank you so much,

 

HHCFX

 


data have;
    infile datalines dlm="|";

length var $ 30000;
input var $;
datalines;
<"myword myword: []/'\":+!@#$%& -0*&^% myword><W15ySpnsrCW1sA5ZZ0</W1urvA5ySpnsrCW1sA5ZZD><W1:RA5prozzznmUnzzozMA5ozW1dW1ozW1><W1:MA5oZozA5m></W1:MA5ozW1DW1ozW1Mrup><W1:MA5ozW1DW1ozW1Mrup MA5ozW1DW1ozW1MrupNW1mA5="A5CW1SA5_CS_MW1"><W1W1ozW1ZZ A5lA5mA5nozNW1mA5="WRKLW1D_OZYPA5_CDA5">myword</W1W1ozW1ZZoA5m><W1W1ozW1ZZozA5m A5lA5mA5nozNW1mA5="WRKLW1D_P_OZYPA5_CDA5"></1W1ozW1ZZozA5m></W1:MA5ozW1DW1ozW1Mrup></W1:RA5prozzznmUnzzozMA5ozW1dW1ozW1><WMW1zzlzznmW1ddrAW1ddr1>20OZ 11W1</W1:W1ddr1><W1:W1ddr3>NW1RRW1MW1NSA5OZOZ, RZZ -</W1:W1ddr3>myword
; run;
1 ACCEPTED SOLUTION

Accepted Solutions
r_behata
Barite | Level 11
data have;
	infile datalines dlm="|";
	length var $ 30000;
	input var $;
	datalines;
<b:ReportingUnit xmlns:a=""><a:Bilingual>0</a:Bilingual><a:Cohort>3</a:Cohort><a:CONTACT_STRATEGY>1</a:SolicitationData></b:ReportingUnit>
;
run;

data want;
	set have;
	length nvar1-nvar9 $10. nvar10 $100.;
	array nvar[*] $ nvar1-nvar10;
	j=1;

	do i=1 to 9;
		nvar[i]=cats(substrn(var,j,10),' ');
		j+10;
	end;

	j+10;
	nvar[i]=substrn(var,j);
run;

Is this what you are looking for ?

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20

Can you also post the expected output?

r_behata
Barite | Level 11
data have;
	infile datalines dlm="|";
	length var $ 30000;
	input var $;
	datalines;
<b:ReportingUnit xmlns:a=""><a:Bilingual>0</a:Bilingual><a:Cohort>3</a:Cohort><a:CONTACT_STRATEGY>1</a:SolicitationData></b:ReportingUnit>
;
run;

data want;
	set have;
	length nvar1-nvar9 $10. nvar10 $100.;
	array nvar[*] $ nvar1-nvar10;
	j=1;

	do i=1 to 9;
		nvar[i]=cats(substrn(var,j,10),' ');
		j+10;
	end;

	j+10;
	nvar[i]=substrn(var,j);
run;

Is this what you are looking for ?

ballardw
Super User

@hhchenfx wrote:

Hi Everyone,

 

I have a long text variable and I want to split this variables in multiple variables (v1 to Vn) with equal number of character in each variables. Of course, the last variable should has the left-over.

 

In the example below, can you please help me to create variables with (10 letter and space) in each?

 

Thank you so much,

 

HHCFX

 


data have;
    infile datalines dlm="|";

length var $ 30000;
input var $;
datalines;
<b:ReportingUnit xmlns:a=""><a:Bilingual>0</a:Bilingual><a:Cohort>3</a:Cohort><a:CONTACT_STRATEGY>1</a:SolicitationData></b:ReportingUnit>
;
run;

so with a defined length of 30000 are you asking to create 3,000 variables each holding 10 characters? Or in this case since your example string is 139 characters 14 variables?

 

And is your space supposed to be at the beginning of the variable? Trailing spaces generally don't mean much in SAS variables.

One does wonder why the magic value of 10 letters and what having 3000 variables is for.

Reeza
Super User
What about values that end up splitting a field? Is this ok:


Line 20: ......... <a: Bilingual
Line 21:>0</a:Bilingual>

Note where the 0 and closing > ends up.
Ksharp
Super User
data have;
	infile datalines dlm="|";
	length var $ 30000;
	input var $;
	datalines4;
<b:ReportingUnit xmlns:a=""><a:Bilingual>0</a:Bilingual><a:Cohort>3</a:Cohort><a:CONTACT_STRATEGY>1</a:SolicitationData></b:ReportingUnit>
;;;;
run;

data temp;
 set have;
 n+1;
 length temp $ 4;
 do i=1 to length(var) by 4;
  temp=substr(var,i);
  output;
 end;
run;

proc print;run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1569 views
  • 0 likes
  • 6 in conversation