Hi Everyone,
I have below data and I want to split the variable "VAR" into multiple variables after length of 9 .So after every 9th position in the string the sentence should split cutting words is fine. Please have a look at below datasets .Thank you in advance.
data have;
var="I want this text to split into variables with same length";
run;
Expected output:
var1 var2 var3 var4 var5 var6 var7
I want th is text to split into vari ables wit h same le ngth
Try this
data have;
var="I want this text to split into variables with same length";
run;
data temp;
set have;
l = length(var);
f = 1;
do while (1);
w = substr(var, f, 9);
id + 1;
output;
f + 9;
if f + 9 > l then do;
w = substr(var, f);
id + 1;
output;
leave;
end;
end;
run;
proc transpose data = temp out = want(drop = _:) prefix = var;
id id;
var w;
run;
Result:
var1 var2 var3 var4 var5 var6 var7 I want th is text t o split i nto varia bles with same len gth
Try this
data have;
var="I want this text to split into variables with same length";
run;
data temp;
set have;
l = length(var);
f = 1;
do while (1);
w = substr(var, f, 9);
id + 1;
output;
f + 9;
if f + 9 > l then do;
w = substr(var, f);
id + 1;
output;
leave;
end;
end;
run;
proc transpose data = temp out = want(drop = _:) prefix = var;
id id;
var w;
run;
Result:
var1 var2 var3 var4 var5 var6 var7 I want th is text t o split i nto varia bles with same len gth
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.