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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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


 

View solution in original post

8 REPLIES 8
novinosrin
Tourmaline | Level 20

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;

 

amyk
Fluorite | Level 6

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. 

novinosrin
Tourmaline | Level 20

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. 

amyk
Fluorite | Level 6

Thank you for your help. The help I received from Reeza was exactly what I was looking for.

Reeza
Super User
You can increment in increments of 5 using a BY within the loop.

do i=1 to &maxnum by 5;
vars = substr(attrib_list, i, 5);
output; *write out as a record;
end;
amyk
Fluorite | Level 6

I'm trying to create the var incrementing everytime a loop passes through and not incrementing the loop

Reeza
Super User

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


 

amyk
Fluorite | Level 6

This worked perfectly. Thank you so so much 🙂

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