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

Hi all ,

 

I have another query  . How can I obtain this 

I am looking to get the most recent "group" value before the "group" value became "Unknown"

 

data have;
informat dates date9.;
format dates date9.;
input dates policy status :$8. group $20.;
datalines;
24APR2018 81341802 ACTIVE Silver
17DEC2018 81341802 ACTIVE Gold
14MAR2019 81341802 ACTIVE Gold
25MAR2019 81341802 CEASED Unknown
24JUL2019 81341802 CEASED Unknown
11SEP2019 81341802 CEASED Unknown
24SEP2019 81341802 CEASED Unknown
08JUL2016 81341828 ACTIVE Gold
09JUL2016 81341828 ACTIVE Gold
01APR2017 81341828 CEASED Unknown
01APR2018 81341828 CEASED Unknown
17DEC2018 81341899 ACTIVE Silver
14MAR2019 81341899 ACTIVE Bronze
25MAR2019 81341899 CEASED Unknown
14MAR2019 81346142 ACTIVE Platinum
25MAR2019 81346142 CEASED Unknown
;
run;
want;
14MAR2019 81341802  Gold
09JUL2016 81341828  Gold
14MAR2019 81341899  Bronze
14MAR2019 81346142  Platinum

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Provided your data is representable:

 

data want(drop=s p);
   merge have
         have(firstobs=2 rename=(status=s policy=p) keep=status policy);
   if policy=p & status ne s & s = "CEASED";
run;

View solution in original post

12 REPLIES 12
PaigeMiller
Diamond | Level 26

Explain the logic used to go from HAVE to WANT.

--
Paige Miller
dennis_oz
Quartz | Level 8

if a policy is "Ceased" I want the last "group" value when the policy was in "Active" state 

PaigeMiller
Diamond | Level 26
data want;
    set have;
	prev_date=lag(dates);
	prev_policy=lag(policy);
	prev_status=lag(status);
	prev_group=lag(group);
	if policy=prev_policy and status="CEASED" and prev_status="ACTIVE" then output;
	drop dates policy status group;
	format prev_date date9.;
run;
--
Paige Miller
novinosrin
Tourmaline | Level 20

 
data have;
informat dates date9.;
format dates date9.;
input dates policy status :$8. group $20.;
datalines;
24APR2018 81341802 ACTIVE Silver
17DEC2018 81341802 ACTIVE Gold
14MAR2019 81341802 ACTIVE Gold
25MAR2019 81341802 CEASED Unknown
24JUL2019 81341802 CEASED Unknown
11SEP2019 81341802 CEASED Unknown
24SEP2019 81341802 CEASED Unknown
08JUL2016 81341828 ACTIVE Gold
09JUL2016 81341828 ACTIVE Gold
01APR2017 81341828 CEASED Unknown
01APR2018 81341828 CEASED Unknown
17DEC2018 81341899 ACTIVE Silver
14MAR2019 81341899 ACTIVE Bronze
25MAR2019 81341899 CEASED Unknown
14MAR2019 81346142 ACTIVE Platinum
25MAR2019 81346142 CEASED Unknown
;
run;
proc format;
 value $gem
 'Unknown'='0'
 other='1';
run;

data want;
 do until(last.group);
  set have;
  by groupformat group notsorted;
  if group ne 'Unknown';
  format group gem.;
 end;
run;

proc datasets lib=work noprint;
 modify want;
 format group;
run;

proc print noobs;run;
novinosrin
Tourmaline | Level 20

Sorry @dennis_oz  I was experimenting the puzzle in my previous before you explained the logic. The Status variable makes it easy-

data want;
 do until(last.status);
  set have;
  by  status notsorted;
  if group ne 'Unknown';
 end;
run;

in  

dennis_oz
Quartz | Level 8

hi @novinosrin , cn you explain what " notsorted   " does ?

novinosrin
Tourmaline | Level 20

NOTSORTED is an option in "By group" processing that handles a set of input data that is not ordered in a particular collating sequence rather by the pattern of existing values. In other words, the existing values as-is until changes becomes a group of its own.

 

Recommended reading: BY GROUP processing

 

If you have author Ron Cody's book, even better 

dennis_oz
Quartz | Level 8

 
data have;
informat dates date9.;
format dates date9.;
input dates policy status :$8. group $20.;
datalines;
24APR2018 81341802 ACTIVE Silver
17DEC2018 81341802 ACTIVE Gold
14MAR2019 81341802 ACTIVE Gold
08JUL2016 81341828 ACTIVE Gold
09JUL2016 81341828 ACTIVE Gold
01APR2017 81341828 CEASED Unknown
01APR2018 81341828 CEASED Unknown
17DEC2018 81341899 ACTIVE Silver
14MAR2019 81341899 ACTIVE Bronze
25MAR2019 81341899 CEASED Unknown
14MAR2019 81346142 ACTIVE Platinum
25MAR2019 81346142 CEASED Unknown
14MAR2019 81346142 ACTIVE Platinum
25MAR2019 81346142 SUSPENDED Platinum
;
run;

Hi @novinosrin ,

suppose the data is in the above format ..

1) where a policy can be "active" as well and not have group as 'Unknown' . 

2) suppose policy can be in "suspended" and group will have some value  but not "unknown" then how can we handle this 

 

novinosrin
Tourmaline | Level 20

Hi @dennis_oz  Can you include your expected result plz to avoid guesses-

Are you after this?

 


data have;
informat dates date9.;
format dates date9.;
input dates policy status :$8. group $20.;
datalines;
24APR2018 81341802 ACTIVE Silver
17DEC2018 81341802 ACTIVE Gold
14MAR2019 81341802 ACTIVE Gold
08JUL2016 81341828 ACTIVE Gold
09JUL2016 81341828 ACTIVE Gold
01APR2017 81341828 CEASED Unknown
01APR2018 81341828 CEASED Unknown
17DEC2018 81341899 ACTIVE Silver
14MAR2019 81341899 ACTIVE Bronze
25MAR2019 81341899 CEASED Unknown
14MAR2019 81346142 ACTIVE Platinum
25MAR2019 81346142 CEASED Unknown
14MAR2019 81346142 ACTIVE Platinum
25MAR2019 81346142 SUSPENDED Platinum
;
run;

data want;
 set have;
 by status notsorted;
 if last.status and status='ACTIVE';
run;
dennis_oz
Quartz | Level 8
my bad . so sorry . my expected output is .

want;
14MAR2019 81341802 ACTIVE Gold
14MAR2019 81341899 ACTIVE Bronze
14MAR2019 81346142 ACTIVE Platinum
25MAR2019 81346142 SUSPENDED Platinum

I always want a "group" value and it should never be "unknown" .
PeterClemmensen
Tourmaline | Level 20

Please explain the logic..

PeterClemmensen
Tourmaline | Level 20

Provided your data is representable:

 

data want(drop=s p);
   merge have
         have(firstobs=2 rename=(status=s policy=p) keep=status policy);
   if policy=p & status ne s & s = "CEASED";
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 12 replies
  • 1446 views
  • 3 likes
  • 4 in conversation