Solved
New Contributor
Posts: 2

Input records of data file into specific array elements.

I'm sure this must be easy; but I can't figure out how to do it.

I have a data file with several thousand records; 1 record per line. Each record has a variable "i" and a variable "x".

I wish to read the input file, and put "x" into a 1-dimensional array where the array element is identified by "i"; and some values of "i" are missing (e.g., i = 1, 2, 3, 5, 6, 10, ...).

Can anybody help?

Accepted Solutions
Solution
‎07-22-2015 03:54 PM
Super User
Posts: 8,069

Re: Input records of data file into specific array elements.

It is already a one dimensional array.

Did you want to convert it into a dataset with one observation and N columns?

Did you want to convert it into a dataset with one variable and N rows?

In either case did you want to insert the missing values for the cells not mentioned in the original data?

Do you know the domain of values for I so that you can set the boundaries for this "array"?  Or do you want to dynamically determine them from the data?

Let's assume that you DO know the boundaries you can make a dataset with one row and N columns very easily.

data want ;

do until (eof);

infile 'myfile' end=eof ;

input i x @@ ;

array out x1-x100 ;

if 1 <= i <= dim(out) then out(i)=x;

else put 'Invalid value for I. ' i= x= ;

end;

drop i x ;

run;

All Replies
Posts: 3,852

Re: Input records of data file into specific array elements.

Are you asking if you can use an array reference in an INPUT statement?

data ar;
infile cards missover;

array y[10];
do while(1);
input i @;
if missing(i) then leave;
else input y @;
end;

drop i;
cards;
10 77 1 44 5 44 2 6 3 8
1 99 10 99 2 8
;;;;
run;

New Contributor
Posts: 2

Re: Input records of data file into specific array elements.

Thank you very much for your help.

It turns out that the suggestion provided by Tom is the most appropriate for my specific situation/application.

Cheers.

Solution
‎07-22-2015 03:54 PM
Super User
Posts: 8,069

Re: Input records of data file into specific array elements.

It is already a one dimensional array.

Did you want to convert it into a dataset with one observation and N columns?

Did you want to convert it into a dataset with one variable and N rows?

In either case did you want to insert the missing values for the cells not mentioned in the original data?

Do you know the domain of values for I so that you can set the boundaries for this "array"?  Or do you want to dynamically determine them from the data?

Let's assume that you DO know the boundaries you can make a dataset with one row and N columns very easily.

data want ;

do until (eof);

infile 'myfile' end=eof ;

input i x @@ ;

array out x1-x100 ;

if 1 <= i <= dim(out) then out(i)=x;

else put 'Invalid value for I. ' i= x= ;

end;

drop i x ;

run;

Super User
Posts: 6,751

Re: Input records of data file into specific array elements.

Here's an approach that works when you already have a SAS data set:

proc transpose data=have prefix='X' out=want (drop=_name_);

var x;

id i;

run;

The results can be different than the other proposed solution in a couple of ways.  (1) This program will generate an error if you have two observations with the same X value.  It's not clear whether that's a good or a bad outcome, but it's an alternative.  And (2) This program will only create the variables that are needed.  If there is no observation having i=2, for example, it does not create a variable named X2.  Again, you have to choose whether that's good or bad.

Good luck.

🔒 This topic is solved and locked.