There is a data like below
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
And using variables x1, x2, x3, I want to assign values of each row of data above. I mean 1-10 is assigned to x1, 11-20 to x2, 21-30 to x3. but I don't know how to do. I tried using @@, but that wasn't what i want to get.
Could you explain a method?
I don't know how to solve the problem in one data step, but with proc transpose after reading the data:
data have;
input z1-z10;
datalines;
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
;
proc transpose data=have out=want(drop=_name_) prefix=x;
var z1-z10;
run;
I don't know how to solve the problem in one data step, but with proc transpose after reading the data:
data have;
input z1-z10;
datalines;
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
;
proc transpose data=have out=want(drop=_name_) prefix=x;
var z1-z10;
run;
Thank you for your help! Your answer is magic to me!
It is difficult to give a complete explanation, but in this case, the structure of the observations and the structure of the variables are transposed, so it would be difficult to import them directly in sas.
It is true that the postposition @@ is used when a single input row contains the values of multiple observations, but it is assumed that the combination of variables is also lined up horizontally.
It is easy to visualize this by executing the following
data have;
input x1 x2 x3 @@;
datalines;
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
;
run;
It might be possible to combine line pointer control and column pointer control (I couldn't do it), but I think it would be quite complicated, so it would be more reliable and easier to execute transpose after inputting as follows.
data have;
input x1-x10;
datalines;
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
;
run;
proc transpose data=have out=want(drop=_name_) prefix=x;
run;
Thank you for your detail answer. And I appreciate for your endeavor to help me!
Hello @Minho_Kang and welcome to the SAS Support Communities!
You can also use an array:
data want(drop=t: j);
array t[3,10];
input t[*];
do j=1 to dim2(t);
x1=t[1,j];
x2=t[2,j];
x3=t[3,j];
output;
end;
cards;
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
;
It sounds like you are trying to get SAS to operate differently, functioning like a vector-based language such as Python. Base SAS software won't work well in that way. I can give you two suggestions.
First, take a look at SAS IML. It is probably exactly what you are looking for.
Second, give us more detail about how this data transformation would help you. What would you do next? You will probably get tons of suggestions about how you can get to your final destination using a totally different route than you are anticipating.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.