Hello team,
I have a code and I want to write a code that finds the max amount before the last amount in do until loop? I can eye ball and say what it is. How can I do this by writing code?
Thanks,
Blue Blue
data salyr; input salary year; cards; 134 2019 run; data increaseit; set salyr; do until (salary gt 1000); salary = salary + salary * 0.06; year = year + 1; output; end; Run;
Make the following change to the second part.
Instead of
if _n_=2 then flg=1;
use
if _n_=2 then output;
The want data set will have only one record. I am giving a sample code below
data salary;
input salary year;
datalines;
200 1990
;
run;
data increaseit;
set salary;
do until (salary gt 1000);
salary = salary + salary * 0.06;
year = year + 1;
if not(salary gt 1000) then output;
end;
Run;
proc sort data=increaseit out=temp;
by descending year;
run;
data want;
set temp;
by descending year;
if _n_=2 then output;
run;
proc print data=want;
run;
The out put will
For example.
data increaseit;
set salyr;
do until (salary gt 1000);
salary = salary + salary * 0.06;
year = year + 1;
if not(salary gt 1000) then output;
end;
Run;
How about this two codes?
Store in other variable for the last observation
data increaseit;
set salyr;
do until (salary gt 1000);
salary = salary + salary * 0.06;
year = year + 1;
output;
salary2=salary;
end;
Run;
Sort and flag the second one.
proc sort data=increaseit out=temp;
by descending year;
run;
data want;
set temp;
by descending year;
if _n_=2 then flg=1;
run;
proc sort data=want;
by year;
run;
Make the following change to the second part.
Instead of
if _n_=2 then flg=1;
use
if _n_=2 then output;
The want data set will have only one record. I am giving a sample code below
data salary;
input salary year;
datalines;
200 1990
;
run;
data increaseit;
set salary;
do until (salary gt 1000);
salary = salary + salary * 0.06;
year = year + 1;
if not(salary gt 1000) then output;
end;
Run;
proc sort data=increaseit out=temp;
by descending year;
run;
data want;
set temp;
by descending year;
if _n_=2 then output;
run;
proc print data=want;
run;
The out put will
Hello @GN0001
Do you want the max value or the total sum?
Your code is creating a sum. The following code will help in deciding what you need sum or max value.
proc sql;
select max(weight) as MAX_WT, sum(Weight) as SUM_WT from sashelp.class;
quit;
The output will be like this
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.