BookmarkSubscribeRSS Feed
devarayalu
Fluorite | Level 6

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;

7 REPLIES 7
shivas
Pyrite | Level 9

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

devarayalu
Fluorite | Level 6

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

Haikuo
Onyx | Level 15

How about this:

data one;

length variety $15.;

do until (variety='Ginger');

set two;

newid=6||id;

output;

end;

stop;

run;

Haikuo

devarayalu
Fluorite | Level 6

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

Haikuo
Onyx | Level 15

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

PGStats
Opal | Level 21

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
Alpay
Fluorite | Level 6

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 = '';).

data one;

length NewId $15;

set two;

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

else NewId = Id;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1273 views
  • 0 likes
  • 5 in conversation