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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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