BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

Appreciate if someone of you help me to write the 'case when ...' statement to derive the variable 'KHIREF' from the following data step?

 

data  WORK.INSURANCE_POLICY_0002(drop=POLICY_NO checkdigit x_source_system_cd);
  set  WORK.INSURANCE_POLICY_0002;
  format checkdigit z2.;
if POLICY_NO ^= '' then do;     
/* combine source code and policy number */
khiref=substr(left(x_source_system_cd),2,1)!!substr(left(policy_no),1,2)!!substr(left(policy_no),4,7);
/* calculate the checkdigit modulo 97. replace 0 by 97 */
checkdigit=compress(mod(khiref,97));
if checkdigit=0 then checkdigit=97;
/* add the checkdigit */
khiref=substr(left(khiref),1,10)!!vvalue(checkdigit);
end;
run;

Sample data as follows:

 

data WORK.INSURANCE_POLICY_0002;
/*length POLICY_NO $20.;*/
POLICY_NO='94_0006288';
run;

 

 

3 REPLIES 3
Kurt_Bremser
Super User

Your data step needs fixing:

32         data  WORK.INSURANCE_POLICY_0002(drop=POLICY_NO checkdigit x_source_system_cd);
33           set  WORK.INSURANCE_POLICY_0002;
34           format checkdigit z2.;
35         if POLICY_NO ^= '' then do;
36         /* combine source code and policy number */
37         khiref=substr(left(x_source_system_cd),2,1)!!substr(left(policy_no),1,2)!!substr(left(policy_no),4,7);
38         /* calculate the checkdigit modulo 97. replace 0 by 97 */
39         checkdigit=compress(mod(khiref,97));
40         if checkdigit=0 then checkdigit=97;
41         /* add the checkdigit */
42         khiref=substr(left(khiref),1,10)!!vvalue(checkdigit);
43         end;
44         run;

NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
      37:20   39:21   
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      39:12   39:25   
NOTE: Variable x_source_system_cd is uninitialized.

Given the necessary intermediate steps, I'd recommend to stay with the data step, it's easier to read and maintain for such kind of logic.

Babloo
Rhodochrosite | Level 12
I have to implement this logic in DI studio and I was asked to use only
'case when'

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—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
  • 3 replies
  • 3532 views
  • 0 likes
  • 3 in conversation