I am having a difficult time finding a way to loop through a string a long string with no delimiters and having a variable with every 5 positions in the string in its own column.
say I have a string the is 'D78fd87fda8787fads0f7df87fa87f9d8fa79d8f' I would need to break off ever 5 character in its own column
I have created a loop which I end up with the last 5 poistion in a column I am trying to figure out how I can increment the Var every time it loops through to get all the data:
data temp;
set temp;
i = 1;
J = 1;
do i = 1 to &maxnum;
vars = substr(attrib_list, J, 5) ;
J= J+5;
end;
run;
None of that matters. All you need is to ensure that it's always an length that's a multiple of 5.
*create fake sample data;
data have;
infile cards truncover;
input myVar $255.;
cards;
D78fd87fda8787fads0f7df87fa87f9d8fa79d8f
Fr34327fda878884ds0f7df87fa8734332a79d8g
;;;;
run;
data want;
set have;
Obs = _n_; *record number to know which go together;
len = length(myVar); *legnth of variable to loop only as needed;
do i=1 to len by 5; *loop;
value = substr(myVar, i, 5); * extract string of size 5 each time;
output; *output;
end;
run;
*wide format;
proc transpose data=want out=wide prefix=VAR;
by Obs;
var value;
run;
@amyk wrote:
I'm trying to create the var incrementing everytime a loop passes through and not incrementing the loop
Are you after something like the results that the following code produces?
data want;
str= 'D78fd87fda8787fads0f7df87fa87f9d8fa79d8f';
array astr [8] $ 5 ;
call pokelong (str , addr (astr[1]) ,vlength(str)) ;
run;
I am using a variable that holds the string, and I won't know how many array elements I need so I am not sure how I can get this to work.
Do string length vary every row? or is it consistent? if it is we can get the array elements at compile time. Nevertheless @Reeza has given a solution that many should understand.
Even the two pass solution
1. loop
2. transpose
will result in some vars having missing values should your string length vary. So should I make a array subscript to (vlength/5) , APP's will be super robust.
Thank you for your help. The help I received from Reeza was exactly what I was looking for.
I'm trying to create the var incrementing everytime a loop passes through and not incrementing the loop
None of that matters. All you need is to ensure that it's always an length that's a multiple of 5.
*create fake sample data;
data have;
infile cards truncover;
input myVar $255.;
cards;
D78fd87fda8787fads0f7df87fa87f9d8fa79d8f
Fr34327fda878884ds0f7df87fa8734332a79d8g
;;;;
run;
data want;
set have;
Obs = _n_; *record number to know which go together;
len = length(myVar); *legnth of variable to loop only as needed;
do i=1 to len by 5; *loop;
value = substr(myVar, i, 5); * extract string of size 5 each time;
output; *output;
end;
run;
*wide format;
proc transpose data=want out=wide prefix=VAR;
by Obs;
var value;
run;
@amyk wrote:
I'm trying to create the var incrementing everytime a loop passes through and not incrementing the loop
This worked perfectly. Thank you so so much 🙂
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.