BookmarkSubscribeRSS Feed
pearsoninst
Pyrite | Level 9

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 ]

datalines;
11 22
12 23
13 24
;
run;

[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

 

data newdata;
Retain name Valu1 Planttype2 name1 ;
input Valu1 Value2;
do name = 'Planttype1';
do i = 1 to 1;
do name1 = 'Planttype2';
do p = 1 to 1;
output;
End;
End;
end;
end;
Drop i p;


datalines;
11 22
12 23
13 24
;
run;

2 REPLIES 2
Tom
Super User Tom
Super User

Huh???

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.

 

kannand
Lapis Lazuli | Level 10

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 ?

Kannan Deivasigamani

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Discussion stats
  • 2 replies
  • 1508 views
  • 1 like
  • 3 in conversation