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;
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.
See the blog post "The SELECT statement in the SAS DATA step."
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!
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.
Ready to level-up your skills? Choose your own adventure.