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?
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
*/
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.
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
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?
/* 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
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
*/
Thanks, Chang! I'll give that a shot as well. I had seen something in my base sas guide about this.
Fantastic. That did it. Thank you!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.