Hi Amir, Sorry for my delay in getting back to you. Here is my program as it stands. I just wanted to know if I could "marcrofy" some bits of it as it quote long winded. *****Notes ***Import Excel worksheet contain code ranges eg - Excel Column called CODE 001-010 050-055 1000-1100 and so on. libname Codes excel "Codelist_LH.xlsx" header = yes mixed = yes; run; data Code_Range; set Codes.'List$'n; where Lvl1Group ne 'Exclusion'; run; *****Read the Variable containing the individual code or code range that has been imported and list out each code individually in a new Variable called INDIV_CODE; data Code_Range1 (drop=BeginCode EndCode); set Code_Range ; BeginCode = input(scan(Code,1),best10.); EndCode = input(scan(Code,2),best10.); if missing(EndCode) then do Indiv_Code=input(Code,best10.);output ;end; else do Indiv_Code = BeginCode to Endcode; output ; end; run; *****Identify number of leading Zeros associated with original variable CODE to later assign to numeric variable INDIV_CODE ; Data Code_Zero; set Code_Range1; Check_for_005=find(code,'00000'); Check_for_004=find(code,'0000'); Check_for_003=find(code,'000'); Check_for_002=find(code,'00'); Check_for_001=find(code,'0'); run; *****Take the correct number of leading zeros per code and create a variable CODEFINAL containing the correct number of zeros ; data Code_Zero1; set Code_Zero; if Check_for_001=1 and Check_for_002=0 and Check_for_003=0 and Check_for_004=0 and Check_for_005=0 then do Codefinal=substr(Code,1,1.);end;else; if Check_for_002=1 and Check_for_003=0 and Check_for_004=0 and Check_for_005=0 then do Codefinal=substr(Code,1,2.);end;else; if Check_for_003=1 and Check_for_004=0 and Check_for_005=0 then do Codefinal=substr(Code,1,3.);end;else; if Check_for_004=1 and Check_for_005=0 then do Codefinal=substr(Code,1,3.);end; run; *****Concatenate the variable CODEFINAL (contains zeros) and INDIV_CODE contains original code minus leading zeros; data Num_Code (drop= check_for_001 check_for_002 check_for_003 check_for_004 check_for_005 Indiv_Code codefinal) ; set Code_Zero1; Numcode=trim(left(codefinal))||trim(left(Indiv_Code)); run; *****Concatenate Character Prefix or Suffix to the variable Numcode ; data Char_Code; set Num_Code; if missing(Prefix_) and missing(Suffix) then do Complete_Code=Numcode;end; else; if Prefix_ ne " " then do Complete_Code=trim(left(Prefix_))||trim(left(Numcode));end; else if Suffix ne " " then do Complete_Code=trim(left(Numcode))||trim(left(Suffix));end; Run; *****Drop Prefix and Suffix Columns and rename Complete_Code; Data Final_Code (drop=numcode prefix_ suffix Complete_Code); set Char_Code; code= Complete_Code; Run;
... View more