Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

Why this do until loop using all the observations?

Reply
Contributor
Posts: 49

Why this do until loop using all the observations?

data two;

input Id $    No   Variety $18-26      Quantity   ;

cards;

240W    15743    Protea          120    

240W    15743    Protea          180    

356W    15744    Heliconia         .    

356W    15744    Anthurium       300    

188R    15747    Ginger           24    

188R    15747    Anthurium        24    

240W    15748    Heliconia        48    

240W    15748    Protea           48    

356W    15748                      .

data one;

set two;

do i=id until (variety='Ginger');

newid=6||id;

end;

proc print;

run;

Super Contributor
Posts: 349

Re: Why this do until loop using all the observations?

Posted in reply to devarayalu

Hi,

Instead of until use while .

data one;

set two ;

do i=id while (variety='Ginger');

newid=6||id;

end;

run;

From SAS documentation.The DO WHILE statement executes statements in a DO loop repetitively while a condition is true, checking the condition before each iteration of the DO loop. The DO UNTIL statement evaluates the condition at the bottom of the loop; the DO WHILE statement evaluates the condition at the top of the loop. 

Note:   The statements in a DO UNTIL loop always execute at least one time, whereas the statements in a DO WHILE loop do not iterate even once if the condition is false.

Thanks,

Shiva

Contributor
Posts: 49

Re: Why this do until loop using all the observations?

Thank you for quick reply but I want the action (addition of 6 to id) for all the first six observations but not  only for the 6th observation. Can U please guide?

Thanks

Respected Advisor
Posts: 3,156

Re: Why this do until loop using all the observations?

Posted in reply to devarayalu

How about this:

data one;

length variety $15.;

do until (variety='Ginger');

set two;

newid=6||id;

output;

end;

stop;

run;

Haikuo

Contributor
Posts: 49

Re: Why this do until loop using all the observations?

Thanks Haikuo

One more request,

I want whole data set but only the first 5 observations with the required action. But not the 5 observations data set.

Can u please do it?

Thanks

Respected Advisor
Posts: 3,156

Re: Why this do until loop using all the observations?

Posted in reply to devarayalu

Try this:

data two;

input Id $    No   Variety $18-26      Quantity   ;

cards;

240W    15743    Protea          120   

240W    15743    Protea          180   

356W    15744    Heliconia         .   

356W    15744    Anthurium       300   

188R    15747    Ginger           24   

188R    15747    Anthurium        24   

240W    15748    Heliconia        48   

240W    15748    Protea           48   

356W    15748                      .

;

data one;

length variety $15.;

do until (variety='Ginger');

set two;

if _n_=1 then newid=6||id;

else call missing(newid);

output;

end;

run;

proc print;run;

Haikuo

Respected Advisor
Posts: 4,919

Re: Why this do until loop using all the observations?

Posted in reply to devarayalu

It is difficult to figure out what is required exactly, but I don't see the need for a DO loop at all. Is this what you want :

data two;
input Id $    No   Variety $18-26      Quantity   ;
cards;
240W    15743    Protea          120   
240W    15743    Protea          180   
356W    15744    Heliconia         .   
356W    15744    Anthurium       300   
188R    15747    Ginger           24   
188R    15747    Anthurium        24   
240W    15748    Heliconia        48   
240W    15748    Protea           48   
356W    15748                      .
;

data one;
length variety $15.;
set two;
newid = 6||id;
output;
if upcase(variety) = "GINGER" then stop;
run;

proc print; run;

PG

PG

PG
Frequent Contributor
Posts: 95

Re: Why this do until loop using all the observations?

Posted in reply to devarayalu

If I understand it right you want Id preceeded by 6 to form NewId for all observations where Variety not equal to 'Ginger'.

What do you want NewId to be when Variety is 'Ginger'?

Below code assigns Id to NewId when Variety is 'Ginger'.

If you want NewId to be null string when Variety is 'Ginger' replace Id with '' in the assignment (else NewId = ''Smiley Wink.

data one;

length NewId $15;

set two;

if NOT(Variety = "Ginger") then NewId=CATS('6',id);

else NewId = Id;

run;

Ask a Question
Discussion stats
  • 7 replies
  • 544 views
  • 0 likes
  • 5 in conversation