DATA Step, Macro, Functions and more

Re: Do loop and Array

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 108
Accepted Solution

Re: Do loop and Array

Hi LainieH,
Very Sorry , i think this is the 2nd time i did this .I dont know how i forgot ? . I will remember . Thank you . Kanchan

Accepted Solutions
Solution
‎11-12-2015 02:09 AM
Super User
Posts: 5,518

Re: Do loop and Array

Posted in reply to pearsoninst

The number of elements in the array cannot change from one observation to the next.  If the length of an account number can vary from one observation to the next, you need to define an array with enough elements to hold the longest account number.

 

Here's a small change that can end your DO loop once all the characters are read from ACCNO:

 

if finda > 0 then do i=1 to (length(line) - finda + 1);

 

Also note, your array statement is defining name1-name12 as being 8 characters long.  A simple change can fix that:

 

array namea (12) $ 1 name1-name12;

  

View solution in original post


All Replies
Frequent Contributor
Posts: 108

Do loop and Array

Posted in reply to pearsoninst

Hi all,

 

I need some help on the below code.

 

I wanted to Split the the account numbers , But as the i coun not make the array dynamic and it is creating additional columns.I wanted name1-Name12 should stop when finda gets  the maximum value.Can i create array as per the number which produce by finda . finda is a user variable. could be anything.

 

data newdata;
input Line$ 30.;
finda = anydigit(Line);
if finda > 0 then
accno = substr(Line,finda,10 ) ;
Array namea (12)$ name1-Name12;
do i = 1 to 12;
namea (i)= Substr(accno ,i,1);
end;
datalines;
Citi 2323232
State Bank of india 232323
;
proc print data = newdata;
run;

Community Manager
Posts: 291

Re: Do loop and Array

[ Edited ]
Posted in reply to pearsoninst

@pearsoninst, you have posted your question in the discussion forum that is not about SAS products (this is the discussion board to discuss the new community platform). 

 

I need to move your post to another community.. If you could please click the orange Find a Community box in the right nav and advise which community you think this belongs and I'm happy to move it for you... 

 

Otherwise I can move it to Base SAS Programming (https://communities.sas.com/t5/Base-SAS-Programming/bd-p/programming)

 

Thanks!!  I say this because you will find people will respond more quickly to the topical discussion forums as they are probably not following this forum as closely..

 

 

Respected Advisor
Posts: 4,173

Re: Do loop and Array

Posted in reply to pearsoninst

Why do you want to split up the account numbers into single digits? If you let us know then may be there are other ways to achieve your desired end result. 

Community Manager
Posts: 291

Re: Do loop and Array

Posted in reply to pearsoninst

Hi @pearsoninst, no worries ! Smiley Wink

 

I will move this post to the programming community forum -- I follow this forum very closely but I'm not a SAS expert Smiley Happy

 

Take care, 

 

Lainie

Solution
‎11-12-2015 02:09 AM
Super User
Posts: 5,518

Re: Do loop and Array

Posted in reply to pearsoninst

The number of elements in the array cannot change from one observation to the next.  If the length of an account number can vary from one observation to the next, you need to define an array with enough elements to hold the longest account number.

 

Here's a small change that can end your DO loop once all the characters are read from ACCNO:

 

if finda > 0 then do i=1 to (length(line) - finda + 1);

 

Also note, your array statement is defining name1-name12 as being 8 characters long.  A simple change can fix that:

 

array namea (12) $ 1 name1-name12;

  

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 226 views
  • 1 like
  • 4 in conversation