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?
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;
Are you asking if you can use an array reference in an INPUT statement?
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.
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;
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.