BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GN0001
Barite | Level 11

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;

 

Blue Blue
1 ACCEPTED SOLUTION

Accepted Solutions
Sajid01
Meteorite | Level 14

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 

forum.PNG

 

View solution in original post

6 REPLIES 6
japelin
Rhodochrosite | Level 12

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;
GN0001
Barite | Level 11
Hello kawakami,
Thanks for the response!
This outputs everything less than 1000.
I need only one observation: only the first observation which is less than 1000.
Please let me know if this makes sense.
Respectfully,
Blue Blue
Blue Blue
japelin
Rhodochrosite | Level 12

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;

 

 

 

Sajid01
Meteorite | Level 14

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 

forum.PNG

 

Sajid01
Meteorite | Level 14

 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

Screenshot 2021-05-12 7.48.04 AM.png

GN0001
Barite | Level 11
Thanks for the response.
No, I don't want a sum.
Regards,
Blue Blue
Blue Blue

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 531 views
  • 3 likes
  • 3 in conversation