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;
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
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
How about this:
data one;
length variety $15.;
do until (variety='Ginger');
set two;
newid=6||id;
output;
end;
stop;
run;
Haikuo
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
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
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
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.