BookmarkSubscribeRSS Feed
philjones820
Fluorite | Level 6
data baseball4;
set work.baseball;
array NumVar _numeric_;
do over NumVar;
if NumVar=. then NumVar=0;
end;
format AVG 6.3;
format SLG 6.3;
MIN=SALARY< 500000;
MAX=SALARY<=10000000;
OBP=H+HBP/PA;
format OBP 6.3;
ISO=SLG-AVG;
format ISO 6.3;
SP=SO/PA;
format SP percent7.1;
BA=H/AB;
format BA 6.3;
IF SALARY >=MAX THEN Salary_Description='Filthy Rich';
ELSE Salary_Description='none';
if SALARY < MIN then sal_type ='Less than League Minimum';
/* ELSE if SALARY >= MIN then sal_type='Filthy Rich'; */
ELSE sal_type='middle';
run;

Hi, I'm not getting any errors in my code, but can someone look at the attached assignment and let me know if there is a better or more efficient way to write this according to the assignment criteria. Thanks, much appreciated!

 

 

 

 

5 REPLIES 5
Patrick
Opal | Level 21

What about:

/* half a million or less */
min= (salary <= 500000);
/* at least 10 million */
max= (salary >= 10000000);

 

Now the way you've coded the variables MIN and MAX will have the values 1 or 0. You basically have right from the first equal sign a logical comparison which either returns True or False (which gives you the values 1 or 0.

 

Now have a look what you've done here:

IF SALARY >=MAX THEN Salary_Description='Filthy Rich';

That will give you a wrong result. It should be: 

IF MAX THEN Salary_Description='Filthy Rich';
or alternatively:
IF MAX=1 THEN Salary_Description='Filthy Rich';

 

Also:

When you have a statement like below...

IF SALARY >=MAX THEN Salary_Description='Filthy Rich';

 ...using a variable which hasn't been defined before then be aware that SAS will assign this variable the length of the string you're assigning. What this means: If you then later on in your code have another such assignment where you try to assign a longer string then this string will get truncated. To avoid this: Always define the length of new variables explicitly before you use it using a length statement.

length Salary_Description $40;
Salary_Description='Filthy Rich'

 

And last but not least and not what you're asked to do in your assignment but something I guess this assignment is meant to achieve as a learning experience to prepare you for the next lesson: An efficient way for recoding or grouping which avoids creating all these additional variables is using SAS Formats and Informats. 

proc format;
  value salaryRangeDesc
    low - 500000 = 'Less than League Minimum'
    10000000 - high = 'Filthy Rich'
    other = 'None'
    ;
quit;

data sample;
  do salary= .,0,499999,500000,500001,09999999,10000000,10000001;
    Salary_Description=put(salary,salaryRangeDesc.);
    output;
  end;
  stop;
run;
proc print data=sample;
run;

 

philjones820
Fluorite | Level 6
Quick question-Is there a way to hide the variables MIN and MAX so that they wont show in the output results?
Reeza
Super User
drop min max;

Add a drop statement in your last step and it will drop the variables at the end of the step.
Reeza
Super User

@philjones820 wrote:
data baseball4;
set work.baseball;
array NumVar _numeric_;
do over NumVar;
if NumVar=. then NumVar=0;
end;
format AVG 6.3;
format SLG 6.3;
MIN=SALARY< 500000;
MAX=SALARY<=10000000;
OBP=H+HBP/PA;
format OBP 6.3;
ISO=SLG-AVG;
format ISO 6.3;
SP=SO/PA;
format SP percent7.1;
BA=H/AB;
format BA 6.3;
IF SALARY >=MAX THEN Salary_Description='Filthy Rich';
ELSE Salary_Description='none';
if SALARY < MIN then sal_type ='Less than League Minimum';
/* ELSE if SALARY >= MIN then sal_type='Filthy Rich'; */
ELSE sal_type='middle';
run;

Hi, I'm not getting any errors in my code, but can someone look at the attached assignment and let me know if there is a better or more efficient way to write this according to the assignment criteria. Thanks, much appreciated!

 

 

 

 



Don’t you need to calculate the min/max? 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 5 replies
  • 1876 views
  • 0 likes
  • 3 in conversation