DATA Step, Macro, Functions and more

Do loop in input line

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

Do loop in input line

Hey guys,

Thanks for your help on my last question. My next question is this:

I have a string of numbers (length = 200). Every 10 numbers is a value for a certain variable. What I'd like to do is create 20 separate variables of length 10 -- like var1, var2, ... , var20. These aren't the only variables being made. I have several others already specified, and the variables represent raw data from a flat file.

How would I do this?


Accepted Solutions
Solution
‎05-30-2012 10:34 AM
Regular Contributor
Posts: 241

Re: Do loop in input line

I am not sure if array processing is necessary. An input statement may be all you need. hth.

ods _all_ close;
ods listing;
options nocenter;
 
data one;
  infile cards firstobs=2;
  input id @5 (w1-w5) ($char3. +1) dummy;
cards;
----+----1----+----2----+----3
01  abc def ghi jkl mno 123456
02   b    f g   j l .   124345
;
run;
 
proc print data=one noobs;
run;
/* on lst
id     w1     w2     w3     w4     w5     dummy
1    abc    def    ghi    jkl    mno    123456
2     b       f    g      j l    .      124345
*/

View solution in original post


All Replies
Super User
Posts: 5,424

Re: Do loop in input line

I'm not exactly clear of your requirement, but I think you could just do a do-loop and us the loop-variable in a substr(), which result is assigned to an array bucket, which in turn is connected to "real" variables.

Data never sleeps
Respected Advisor
Posts: 3,156

Re: Do loop in input line

Try this:

data want;

infile "your file";

input var200 :$200. a b;

array var var1-var20;

do i=1 to dim(var);

  var(i)=substr(var200,10*i-9,10);

end;

drop i;

run;

Haikuo

Contributor
Posts: 33

Re: Do loop in input line

What we've got going on in this thread seems to be on the right track. i'm going to be a little more specific.

All variables (inlcuding the ones which we're not creating from the do loop) are found in a string of numbers ranging from 28 to 238. A string of 28 would have var1 = . and a string of 238 would have var1, var2, var3, ... , var21.

The first variable is 10 characters in length. The second is 8. Third is 8. Fourth is 2. This gives a base length of 28. After 28, the string length increases by multiples of 10. So an observation with length = 48 has the following variables (length in parentheses):

name (10)

elig_date (8)

deny_date (8)

specialty (2)

var1 (10)

var2 (10)

The variables are read in in this order. So name has to come first, followed by elig_date, deny_date, specialty, and then the repeated variables.

Both Linlin and Hai.kuo's programs give me proper column names (variables) in the output table, but the values for these columns are not correct. Some are missing, and some have pieces of the other variables in them.

Thoughts?

Super Contributor
Posts: 1,636

Re: Do loop in input line

/* example */

data have;

infile cards;

input all $ 20.;

cards;

112233445566778899

;

data want;

set have;

array _a(*) var1-var9;

do i=1 to 9;

_a(i)=substr(all,2*(i-1)+1,2);

end;

drop i;

proc print;run;

/* for your case */

data want;

set have;

array _a(*) var1-var20;

do i=1 to 20;

_a(i)=substr(all,10*(i-1)+1,10);

end;

drop i;

run;

Linlin

Solution
‎05-30-2012 10:34 AM
Regular Contributor
Posts: 241

Re: Do loop in input line

I am not sure if array processing is necessary. An input statement may be all you need. hth.

ods _all_ close;
ods listing;
options nocenter;
 
data one;
  infile cards firstobs=2;
  input id @5 (w1-w5) ($char3. +1) dummy;
cards;
----+----1----+----2----+----3
01  abc def ghi jkl mno 123456
02   b    f g   j l .   124345
;
run;
 
proc print data=one noobs;
run;
/* on lst
id     w1     w2     w3     w4     w5     dummy
1    abc    def    ghi    jkl    mno    123456
2     b       f    g      j l    .      124345
*/

Contributor
Posts: 33

Re: Do loop in input line

Posted in reply to chang_y_chung_hotmail_com

Thanks, Chang! I'll give that a shot as well. I had seen something in my base sas guide about this.

Contributor
Posts: 33

Re: Do loop in input line

Posted in reply to chang_y_chung_hotmail_com

Fantastic. That did it. Thank you!

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 326 views
  • 6 likes
  • 5 in conversation