11-01-2015 10:18 AM
Hi I have a problem like this.
My dataset contains 2 variables 3 Observation each i want to put Planttype1 and Planttype2 in each observation.
[dateset Variables ]
[I want ]
Obs name Valu1 name1 Value2
1 Planttype1 11 Planttype2 22
2 Planttype1 12 Planttype2 23
3 Planttype1 13 Planttype2 24
This below code is working fine ,but i feel there is to much of code i have used ,how can i optimized this ?hh
Retain name Valu1 Planttype2 name1 ;
input Valu1 Value2;
do name = 'Planttype1';
do i = 1 to 1;
do name1 = 'Planttype2';
do p = 1 to 1;
Drop i p;
11-01-2015 10:51 AM - edited 11-01-2015 10:52 AM
To read your original data.
data have ; input plantype1 plantype2; datalines; 11 22 12 23 13 24 ;
To convert to the new structure from there.
data want ; set have ; name='Plantype1'; name1='Plantype2'; rename plantype1=valu1 plantype2=valu2; run;
The obvious question is WHY would you want to make this change?
Also why name series of variables where the number start being appended on the second name in the series. like X, X1, X2, instead of X1, X2, X3. If you use the later convention then you can use variable lists. Also it will be less confusing to the users of the data.
11-01-2015 10:56 AM
You've mentioned that your data has 2 variables and 3 records each. Does that mean the pattern repeats ? If it doesn't repeat, why do you need a "Do" loop ? If it repeats, do the names remain the same through out as Planttype1 and Planttype2 ?