BookmarkSubscribeRSS Feed
souji
Obsidian | Level 7

Please correct me if I am wrong:

 

/*Scenario:

1. Create a variable, Bonus, that is 5% of Income. If the Bonus is over $7000,  create the variable Total that sums Bonus and Income

2. Only output observations with Bonus greater than $7000 to the New dataset, BonusOver7K,

    there should be no missing value for the Bonus variable in the data set.

3.what is the value of the variable Total for observation 114?

4. what is the grand total of the variable Total for the entire data set?*/

 

data work.BonusOver7K;

set Souj.input24;

Bonus=Income*0.5;

where Bonus ne .;

if Bonus>7000  then Total=sum(Bouns,Income);/* I think,#1*/

if Bonus>7000  then output BonusOver7K;/* I think, #2*/

run;

Proc print data=BonusOver7K;

sum Total; /* I think, #4 */

run;

 

4 REPLIES 4
ed_sas_member
Meteorite | Level 14
Hi @souji
Just a little mistake:
you should multiply by 0.05 to have 5% of the bonus.
You can optimize your code by using ‘do’ ‘end’:

if Bonus>7000 then do;
Total=sum(Bouns,Income);/* I think,#1*/
output BonusOver7K;/* I think, #2*/
End;
run;
Cynthia_sas
SAS Super FREQ
Good catch about the diff between 5% and 50%!
Cynthia
Cynthia_sas
SAS Super FREQ

Hi:

Please review your information about WHERE statements. I would expect that you are getting an ERROR message in your code something like this:

ERROR: Variable Bonus is not on file SOUJ.INPUT24.


Because a WHERE statement can ONLY be used if the variable(s) in the WHERE already exist in the INPUT dataset (in this case SOUJ.INPUT24). And since you are creating BONUS, my assumption is that BONUS does not exist.


Since we do not know what variables you're starting with in SOUJ.INPUT24, it also might be that you DO have BONUS in that input data.


You also have a TYPO here:

if Bonus>7000 then Total=sum(Bouns,Income);


So that is also going to be an issue because I think you meant to create TOTAL by summing BONUS and INCOME, not BOUNS and INCOME.


Remember that if you want to subset based on a variable (or column) you are creating in a program, then you can subset using a syntax construct called a "subsetting IF". Since you did not post any data, this program uses just 6 observations that test all your conditions. Generated values for BONUS fall above 7K and under 7K and one value is missing:

use_subsetting_if.png

 

The subsetting IF will act as a "gate" and if an observation fails the test, processing will stop. That means for any observations where calculated Bonus is missing or under 7K, the observation will never get to the creation of TOTAL or the implicit output at the end of the DATA step program.

 

Hope this helps,

Cynthia

souji
Obsidian | Level 7

Thank you very much for both of you @Cynthia_sas  @ed_sas_member 

for correcting me ... I am still learningSmiley Happy

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 993 views
  • 2 likes
  • 3 in conversation