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

i have doubt in writting code logic 

scenario is i have data set of employees with different job codes of total 500 observation in that task as follows

/**************Task******************/
*Take libref.employees as input
*Calculate bonus as 10% of salary for job code FLTAT1 and
write to output datset FL1
*Calculate bonus as 15% of salary for job code FLTAT2
and write to output datset FL2
*Calculate bonus as 20% of salary for job code FLTAT3 and
write to output datset FL3
*For rest all calculate bonus as 25% of salary and
write to output datset rest;

using conditions

 

im getting the output set upto fl3 but im not getting logic for Rest output dataset .

please help me in that task

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
data fl1 fl2 fl3 rest;
set libref.employees;
select (jobcode);
  when ('FLTAT1') do;
    bonus = salary * .1;
    output fl1;
  end;
  when ('FLTAT2') do;
    bonus = salary * .15;
    output fl2;
  end;
  when ('FLTAT3') do;
    bonus = salary * .2;
    output fl3;
  end;
  otherwise do;
    bonus = salary * .25;
    output rest;
  end;
end;
run;

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User
data fl1 fl2 fl3 rest;
set libref.employees;
select (jobcode);
  when ('FLTAT1') do;
    bonus = salary * .1;
    output fl1;
  end;
  when ('FLTAT2') do;
    bonus = salary * .15;
    output fl2;
  end;
  when ('FLTAT3') do;
    bonus = salary * .2;
    output fl3;
  end;
  otherwise do;
    bonus = salary * .25;
    output rest;
  end;
end;
run;
vempatapu
Calcite | Level 5

thank you bremser thats working buddy.

if your leisure  now .

please let me know how select and when conditions will work .

 

Kurt_Bremser
Super User

The SELECT in a DATA step is the logical equivalent of CASE in PASCAL or SWITCH in C, but it is more versatile than those.

While SWITCH in C does need BREAK to exit a branch, SELECT does this automatically.

Also you can use an empty SELECT () and quite complex conditions for every branch.

 

select ();
  when (sky = 'blue') do; .... end;
  when (water = 'wet') do; .... end;
  otherwise;
end;
vempatapu
Calcite | Level 5
thank you for answer buddy
vempatapu
Calcite | Level 5
thank you bremser thats working buddy.
if your leisure now .
please let me know how select and when conditions will work .
RW9
Diamond | Level 26 RW9
Diamond | Level 26

What does your input data look like, post a datastep with some test data.  You should be able to do it all in one datastep:

data want;
  set have;
  select(job_code);
    when("FLTAT1") bonus=(salary / 100) * 10;
    when("FLTAT2") bonus=(salary / 100) * 15;
    ...
    otherwise;
  end;
run;

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