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

Hi,

 

I'm using SAS Universitiy Edition. Can anyone guide me how to write this in data step based on condition below:

 

If any employee get appl = 'P' and n_indi = 'In' then will be tagged as 'A1' under new_ind. (A1 repeat for subsequent row under same name)

If any employee get appl = 'P' and n_indi = 'Co' then will be tagged as 'A2' under new_ind.  (A2 repeat for subsequent row under same name)

 

My Output: 

 employee_nameappln_indinew_ind

1

AAAPCoA2

2

AAAGuInN

3

AAAGuInN

4

BBBPInA1

5

BBBGuInN

6

BBBGuInN

7

CCCPCoA2

8

CCCPCoN

9

DDDPInA1

10

DDDGuInN

11

EEWGuInN

 

Desired Output:

 employee_nameappln_indinew_ind

1

AAAPCoA2

2

AAAGuInA2

3

AAAGuInA2

4

BBBPInA1

5

BBBGuInA1

6

BBBGuInA1

7

CCCPCoA2

8

CCCPCoA2

9

DDDPInA1

10

DDDGuInA1

11

EEWGuInError

 

My script

 

 

data employee;
input employee_name $ appl $ n_indi $;
datalines;
AAA Gu In
AAA P Co
AAA Gu In
BBB Gu In
BBB P In
BBB Gu In
CCC P Co
CCC P Co
DDD P In
EEE Gu In
FFF Gu In
;
run;

 

proc sort data= employee
out= employee2;
by employee_name descending appl ;
run;

 

data employee3;
set employee2;
by employee_name descending appl;


if first.employee_name and appl = 'P' and n_indi = 'Co' then new_ind = 'HPA2' ;
else if appl = 'P' and n_indi = 'In' then new_ind = 'HPA1' ;
retain new_ind;
else new_ind = 'N';

;run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

This code (which is just a heavily simplified version of yours) will almost do it:

data employee3;
set employee2;
by employee_name descending appl;
retain new_ind;
if appl = 'P' and n_indi = 'Co' then new_ind = 'HPA2' ;
else if appl = 'P' and n_indi = 'In' then new_ind = 'HPA1';
run;

You need to formulate the "error" condition for FFF.

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

This code (which is just a heavily simplified version of yours) will almost do it:

data employee3;
set employee2;
by employee_name descending appl;
retain new_ind;
if appl = 'P' and n_indi = 'Co' then new_ind = 'HPA2' ;
else if appl = 'P' and n_indi = 'In' then new_ind = 'HPA1';
run;

You need to formulate the "error" condition for FFF.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 618 views
  • 1 like
  • 2 in conversation