Community talk about communities.sas.com. Meta!

Do loop

Reply
Frequent Contributor
Posts: 108

Do loop

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;

Super User
Super User
Posts: 7,050

Re: Do loop

[ Edited ]
Posted in reply to pearsoninst

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.

 

Regular Contributor
Posts: 161

Re: Do loop

Posted in reply to pearsoninst

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
Ask a Question
Discussion stats
  • 2 replies
  • 458 views
  • 1 like
  • 3 in conversation