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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
chang_y_chung_hotmail_com
Obsidian | Level 7

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

7 REPLIES 7
LinusH
Tourmaline | Level 20

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
Haikuo
Onyx | Level 15

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

jtrousd
Calcite | Level 5

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?

Linlin
Lapis Lazuli | Level 10

/* 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

chang_y_chung_hotmail_com
Obsidian | Level 7

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
*/

jtrousd
Calcite | Level 5

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

jtrousd
Calcite | Level 5

Fantastic. That did it. Thank you!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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