A Global Divorce Analytics Dataset into Regulatory-Grade Insights Using SAS and R
Shattered Bonds to Structured Intelligence: Engineering a Global Divorce Analytics Dataset into Regulatory-Grade Insights Using SAS and R
Introduction
The
phrase "highest divorce rate in world" often appears in
headlines, social discussions, demographic studies, and public policy reports.
However, behind every global ranking lies something analysts understand well: data
quality determines truth quality.
Imagine a
demographic research organization preparing an international family stability
report for governments and economic policy groups. During final validation,
analysts discover serious problems:
- Countries appearing twice
with different spellings.
- Negative divorce
percentages.
- Missing reporting years.
- Invalid email addresses from
survey agencies.
- Incorrect age values such as
250 years.
- Mixed capitalization in
country names.
- Corrupted regional
classifications.
- Missing population counts.
- Impossible timestamps.
The
executive dashboard suddenly shows impossible conclusions:
- Divorce rates exceeding
150%.
- Countries disappearing from
visualizations.
- AI forecasting models
predicting impossible demographic shifts.
- Statistical outputs becoming
unusable.
This
scenario mirrors challenges faced daily in:
- Clinical trials
- Insurance claims
- Banking risk systems
- Retail analytics
- Pharmacovigilance reporting
Dirty
data creates expensive business mistakes.
Global Divorce Dataset
Structure
Our enterprise
dataset contains 24 observations and 9 variables.
|
Variable |
Description |
|
RECORD_ID |
Unique
identifier |
|
COUNTRY_NAME |
Country
reporting divorce statistics |
|
REGION_CODE |
Geographic
region |
|
REPORT_YEAR |
Reporting
year |
|
DIVORCE_RATE |
Divorce
per 1000 population |
|
MEDIAN_AGE |
Median
divorced age |
|
SURVEY_EMAIL |
Reporting
agency email |
|
POPULATION_M |
Population
in millions |
|
STATUS_FLAG |
Validation
status |
1.Raw SAS Dataset with Intentional Errors
data divorce_raw;
length record_id $8 country_name $40 region_code $15
survey_email $60 status_flag $15;
informat report_year 4. divorce_rate 8.2 median_age 8.
population_m 8.2;
input record_id $ country_name $ region_code $ report_year
divorce_rate median_age survey_email $ population_m status_flag $;
datalines;
D001 Maldives ASIA 2024 3.8 36 data@stats.org 0.52 VALID
D002 Belarus EUROPE 2024 4.1 41 surveyagency.com 9.20 VALID
D003 portugal europe 2024 -2.5 38 info@agency.org 10.40 VALID
D004 Portugal EUROPE . 2.9 38 info@agency.org 10.40 VALID
D005 USA NORTH_AMERICA 2024 2.4 250 census@usa.gov 340 VALID
D006 Denmark EUROPE 2024 3.1 -5 denmark@survey.dk 5.9 VALID
D007 Maldives ASIA 2024 3.8 36 data@stats.org 0.52 VALID
D008 Latvia EUROPE 2024 4.2 39 NULL 1.8 VALID
D009 Belgium EUROPE 2024 2.8 37 contact@@belgium.be 11.7 VALID
D010 canada north_america 2024 2.1 40 canada.ca 39 VALID
D011 Russia EUROPE 2024 3.9 42 data@russia.ru -145 VALID
D012 Spain EUROPE 2024 2.0 39 info@spain.es 48 VALID
D013 INDIA asia 2024 1.1 34 india@gov.in 1430 VALID
D014 Japan ASIA 2024 1.7 43 japan@stats.jp 125 VALID
D015 Germany EUROPE 2024 1.8 44 germany@gov.de 84 VALID
D016 Sweden EUROPE 2024 2.5 39 sweden.gov 10 VALID
D017 Norway EUROPE 2024 1.9 41 norway@stats.no 5.5 VALID
D018 Brazil LATAM 2024 2.2 35 brazil@ibge.br 216 VALID
D019 Mexico LATAM 2024 2.0 37 mexico@@gov.mx 129 VALID
D020 France EUROPE 2024 1.9 40 france@gov.fr 68 VALID
D021 UK EUROPE 2024 2.3 42 uk@stats.uk 68 VALID
D022 Australia OCEANIA 2024 2.1 39 aus@gov.au 26 VALID
D023 Egypt AFRICA 2024 2.5 36 egypt.gov 109 VALID
D024 Turkey EUROPE 2024 2.2 38 turkey@stats.tr 85 VALID
;
run;
proc print data=divorce_raw;
run;
OUTPUT:
| Obs | record_id | country_name | region_code | survey_email | status_flag | report_year | divorce_rate | median_age | population_m |
|---|---|---|---|---|---|---|---|---|---|
| 1 | D001 | Maldives | ASIA | data@stats.org | VALID | 2024 | 3.8 | 36 | 0.52 |
| 2 | D002 | Belarus | EUROPE | surveyagency.com | VALID | 2024 | 4.1 | 41 | 9.20 |
| 3 | D003 | portugal | europe | info@agency.org | VALID | 2024 | -2.5 | 38 | 10.40 |
| 4 | D004 | Portugal | EUROPE | info@agency.org | VALID | . | 2.9 | 38 | 10.40 |
| 5 | D005 | USA | NORTH_AMERICA | census@usa.gov | VALID | 2024 | 2.4 | 250 | 3.40 |
| 6 | D006 | Denmark | EUROPE | denmark@survey.dk | VALID | 2024 | 3.1 | -5 | 5.90 |
| 7 | D007 | Maldives | ASIA | data@stats.org | VALID | 2024 | 3.8 | 36 | 0.52 |
| 8 | D008 | Latvia | EUROPE | NULL | VALID | 2024 | 4.2 | 39 | 1.80 |
| 9 | D009 | Belgium | EUROPE | contact@@belgium.be | VALID | 2024 | 2.8 | 37 | 11.70 |
| 10 | D010 | canada | north_america | canada.ca | VALID | 2024 | 2.1 | 40 | 0.39 |
| 11 | D011 | Russia | EUROPE | data@russia.ru | VALID | 2024 | 3.9 | 42 | -1.45 |
| 12 | D012 | Spain | EUROPE | info@spain.es | VALID | 2024 | 2.0 | 39 | 0.48 |
| 13 | D013 | INDIA | asia | india@gov.in | VALID | 2024 | 1.1 | 34 | 14.30 |
| 14 | D014 | Japan | ASIA | japan@stats.jp | VALID | 2024 | 1.7 | 43 | 1.25 |
| 15 | D015 | Germany | EUROPE | germany@gov.de | VALID | 2024 | 1.8 | 44 | 0.84 |
| 16 | D016 | Sweden | EUROPE | sweden.gov | VALID | 2024 | 2.5 | 39 | 0.10 |
| 17 | D017 | Norway | EUROPE | norway@stats.no | VALID | 2024 | 1.9 | 41 | 5.50 |
| 18 | D018 | Brazil | LATAM | brazil@ibge.br | VALID | 2024 | 2.2 | 35 | 2.16 |
| 19 | D019 | Mexico | LATAM | mexico@@gov.mx | VALID | 2024 | 2.0 | 37 | 1.29 |
| 20 | D020 | France | EUROPE | france@gov.fr | VALID | 2024 | 1.9 | 40 | 0.68 |
| 21 | D021 | UK | EUROPE | uk@stats.uk | VALID | 2024 | 2.3 | 42 | 0.68 |
| 22 | D022 | Australia | OCEANIA | aus@gov.au | VALID | 2024 | 2.1 | 39 | 0.26 |
| 23 | D023 | Egypt | AFRICA | egypt.gov | VALID | 2024 | 2.5 | 36 | 1.09 |
| 24 | D024 | Turkey | EUROPE | turkey@stats.tr | VALID | 2024 | 2.2 | 38 | 0.85 |
Intentional Errors Included
- Duplicate country records
- Missing report year
- Negative divorce rates
- Invalid ages
- Missing emails
- Invalid email format
- Negative population
- Mixed case regions
- NULL string corruption
Why LENGTH Must Appear
First
One of
the most dangerous SAS issues is character truncation risk.
Incorrect:
data demo;
country="United States of
America";
length country $10;
run;
Result:
United Sta
Correct:
data demo;
length country $30;
country="United States of
America";
run;
Unlike
SAS, R dynamically allocates character memory and avoids truncation issues
automatically.
2.Enterprise Cleaning Workflow Using DATA Step
data divorce_clean;
set divorce_raw;
country_name=propcase(strip(country_name));
region_code=upcase(strip(region_code));
if divorce_rate <0 then divorce_rate=abs(divorce_rate);
if median_age<18 or median_age>100 then median_age=.;
if population_m<0 then population_m=.;
if survey_email='NULL' then survey_email='';
if find(survey_email,'@')=0 then
status_flag='INVALID_EMAIL';
if missing(report_year) then
report_year=2024;
status_flag=coalescec(status_flag,'VALID');
run;
proc print data=divorce_clean;
run;
OUTPUT:
| Obs | record_id | country_name | region_code | survey_email | status_flag | report_year | divorce_rate | median_age | population_m |
|---|---|---|---|---|---|---|---|---|---|
| 1 | D001 | Maldives | ASIA | data@stats.org | VALID | 2024 | 3.8 | 36 | 0.52 |
| 2 | D002 | Belarus | EUROPE | surveyagency.com | INVALID_EMAIL | 2024 | 4.1 | 41 | 9.20 |
| 3 | D003 | Portugal | EUROPE | info@agency.org | VALID | 2024 | 2.5 | 38 | 10.40 |
| 4 | D004 | Portugal | EUROPE | info@agency.org | VALID | 2024 | 2.9 | 38 | 10.40 |
| 5 | D005 | Usa | NORTH_AMERICA | census@usa.gov | VALID | 2024 | 2.4 | . | 3.40 |
| 6 | D006 | Denmark | EUROPE | denmark@survey.dk | VALID | 2024 | 3.1 | . | 5.90 |
| 7 | D007 | Maldives | ASIA | data@stats.org | VALID | 2024 | 3.8 | 36 | 0.52 |
| 8 | D008 | Latvia | EUROPE | INVALID_EMAIL | 2024 | 4.2 | 39 | 1.80 | |
| 9 | D009 | Belgium | EUROPE | contact@@belgium.be | VALID | 2024 | 2.8 | 37 | 11.70 |
| 10 | D010 | Canada | NORTH_AMERICA | canada.ca | INVALID_EMAIL | 2024 | 2.1 | 40 | 0.39 |
| 11 | D011 | Russia | EUROPE | data@russia.ru | VALID | 2024 | 3.9 | 42 | . |
| 12 | D012 | Spain | EUROPE | info@spain.es | VALID | 2024 | 2.0 | 39 | 0.48 |
| 13 | D013 | India | ASIA | india@gov.in | VALID | 2024 | 1.1 | 34 | 14.30 |
| 14 | D014 | Japan | ASIA | japan@stats.jp | VALID | 2024 | 1.7 | 43 | 1.25 |
| 15 | D015 | Germany | EUROPE | germany@gov.de | VALID | 2024 | 1.8 | 44 | 0.84 |
| 16 | D016 | Sweden | EUROPE | sweden.gov | INVALID_EMAIL | 2024 | 2.5 | 39 | 0.10 |
| 17 | D017 | Norway | EUROPE | norway@stats.no | VALID | 2024 | 1.9 | 41 | 5.50 |
| 18 | D018 | Brazil | LATAM | brazil@ibge.br | VALID | 2024 | 2.2 | 35 | 2.16 |
| 19 | D019 | Mexico | LATAM | mexico@@gov.mx | VALID | 2024 | 2.0 | 37 | 1.29 |
| 20 | D020 | France | EUROPE | france@gov.fr | VALID | 2024 | 1.9 | 40 | 0.68 |
| 21 | D021 | Uk | EUROPE | uk@stats.uk | VALID | 2024 | 2.3 | 42 | 0.68 |
| 22 | D022 | Australia | OCEANIA | aus@gov.au | VALID | 2024 | 2.1 | 39 | 0.26 |
| 23 | D023 | Egypt | AFRICA | egypt.gov | INVALID_EMAIL | 2024 | 2.5 | 36 | 1.09 |
| 24 | D024 | Turkey | EUROPE | turkey@stats.tr | VALID | 2024 | 2.2 | 38 | 0.85 |
Explanation
This DATA
step represents the core of production healthcare and banking pipelines.
Functions
demonstrated:
- ABS
- STRIP
- PROPCASE
- FIND
- COALESCEC
- UPCASE
A
clinical trial equivalent would include:
- correcting visit dates,
- validating age ranges,
- standardizing treatment
groups,
- ensuring SDTM compliance.
3.Advanced SELECT-WHEN Validation
data divorce_quality;
length risk_level $10;
set divorce_clean;
select;
when(divorce_rate>=4) risk_level='HIGH';
when(divorce_rate>=2) risk_level='MEDIUM';
otherwise risk_level='LOW';
end;
run;
proc print data=divorce_quality;
run;
OUTPUT:
| Obs | risk_level | record_id | country_name | region_code | survey_email | status_flag | report_year | divorce_rate | median_age | population_m |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | MEDIUM | D001 | Maldives | ASIA | data@stats.org | VALID | 2024 | 3.8 | 36 | 0.52 |
| 2 | HIGH | D002 | Belarus | EUROPE | surveyagency.com | INVALID_EMAIL | 2024 | 4.1 | 41 | 9.20 |
| 3 | MEDIUM | D003 | Portugal | EUROPE | info@agency.org | VALID | 2024 | 2.5 | 38 | 10.40 |
| 4 | MEDIUM | D004 | Portugal | EUROPE | info@agency.org | VALID | 2024 | 2.9 | 38 | 10.40 |
| 5 | MEDIUM | D005 | Usa | NORTH_AMERICA | census@usa.gov | VALID | 2024 | 2.4 | . | 3.40 |
| 6 | MEDIUM | D006 | Denmark | EUROPE | denmark@survey.dk | VALID | 2024 | 3.1 | . | 5.90 |
| 7 | MEDIUM | D007 | Maldives | ASIA | data@stats.org | VALID | 2024 | 3.8 | 36 | 0.52 |
| 8 | HIGH | D008 | Latvia | EUROPE | INVALID_EMAIL | 2024 | 4.2 | 39 | 1.80 | |
| 9 | MEDIUM | D009 | Belgium | EUROPE | contact@@belgium.be | VALID | 2024 | 2.8 | 37 | 11.70 |
| 10 | MEDIUM | D010 | Canada | NORTH_AMERICA | canada.ca | INVALID_EMAIL | 2024 | 2.1 | 40 | 0.39 |
| 11 | MEDIUM | D011 | Russia | EUROPE | data@russia.ru | VALID | 2024 | 3.9 | 42 | . |
| 12 | MEDIUM | D012 | Spain | EUROPE | info@spain.es | VALID | 2024 | 2.0 | 39 | 0.48 |
| 13 | LOW | D013 | India | ASIA | india@gov.in | VALID | 2024 | 1.1 | 34 | 14.30 |
| 14 | LOW | D014 | Japan | ASIA | japan@stats.jp | VALID | 2024 | 1.7 | 43 | 1.25 |
| 15 | LOW | D015 | Germany | EUROPE | germany@gov.de | VALID | 2024 | 1.8 | 44 | 0.84 |
| 16 | MEDIUM | D016 | Sweden | EUROPE | sweden.gov | INVALID_EMAIL | 2024 | 2.5 | 39 | 0.10 |
| 17 | LOW | D017 | Norway | EUROPE | norway@stats.no | VALID | 2024 | 1.9 | 41 | 5.50 |
| 18 | MEDIUM | D018 | Brazil | LATAM | brazil@ibge.br | VALID | 2024 | 2.2 | 35 | 2.16 |
| 19 | MEDIUM | D019 | Mexico | LATAM | mexico@@gov.mx | VALID | 2024 | 2.0 | 37 | 1.29 |
| 20 | LOW | D020 | France | EUROPE | france@gov.fr | VALID | 2024 | 1.9 | 40 | 0.68 |
| 21 | MEDIUM | D021 | Uk | EUROPE | uk@stats.uk | VALID | 2024 | 2.3 | 42 | 0.68 |
| 22 | MEDIUM | D022 | Australia | OCEANIA | aus@gov.au | VALID | 2024 | 2.1 | 39 | 0.26 |
| 23 | MEDIUM | D023 | Egypt | AFRICA | egypt.gov | INVALID_EMAIL | 2024 | 2.5 | 36 | 1.09 |
| 24 | MEDIUM | D024 | Turkey | EUROPE | turkey@stats.tr | VALID | 2024 | 2.2 | 38 | 0.85 |
4.ARRAY Validation Example
data validation_check;
set divorce_clean;
array nums divorce_rate median_age population_m;
do i=1 to dim(nums);
if nums(i)<0 then nums(i)=.;
end;
drop i;
run;
proc print data=validation_check;
run;
OUTPUT:
| Obs | record_id | country_name | region_code | survey_email | status_flag | report_year | divorce_rate | median_age | population_m |
|---|---|---|---|---|---|---|---|---|---|
| 1 | D001 | Maldives | ASIA | data@stats.org | VALID | 2024 | 3.8 | 36 | 0.52 |
| 2 | D002 | Belarus | EUROPE | surveyagency.com | INVALID_EMAIL | 2024 | 4.1 | 41 | 9.20 |
| 3 | D003 | Portugal | EUROPE | info@agency.org | VALID | 2024 | 2.5 | 38 | 10.40 |
| 4 | D004 | Portugal | EUROPE | info@agency.org | VALID | 2024 | 2.9 | 38 | 10.40 |
| 5 | D005 | Usa | NORTH_AMERICA | census@usa.gov | VALID | 2024 | 2.4 | . | 3.40 |
| 6 | D006 | Denmark | EUROPE | denmark@survey.dk | VALID | 2024 | 3.1 | . | 5.90 |
| 7 | D007 | Maldives | ASIA | data@stats.org | VALID | 2024 | 3.8 | 36 | 0.52 |
| 8 | D008 | Latvia | EUROPE | INVALID_EMAIL | 2024 | 4.2 | 39 | 1.80 | |
| 9 | D009 | Belgium | EUROPE | contact@@belgium.be | VALID | 2024 | 2.8 | 37 | 11.70 |
| 10 | D010 | Canada | NORTH_AMERICA | canada.ca | INVALID_EMAIL | 2024 | 2.1 | 40 | 0.39 |
| 11 | D011 | Russia | EUROPE | data@russia.ru | VALID | 2024 | 3.9 | 42 | . |
| 12 | D012 | Spain | EUROPE | info@spain.es | VALID | 2024 | 2.0 | 39 | 0.48 |
| 13 | D013 | India | ASIA | india@gov.in | VALID | 2024 | 1.1 | 34 | 14.30 |
| 14 | D014 | Japan | ASIA | japan@stats.jp | VALID | 2024 | 1.7 | 43 | 1.25 |
| 15 | D015 | Germany | EUROPE | germany@gov.de | VALID | 2024 | 1.8 | 44 | 0.84 |
| 16 | D016 | Sweden | EUROPE | sweden.gov | INVALID_EMAIL | 2024 | 2.5 | 39 | 0.10 |
| 17 | D017 | Norway | EUROPE | norway@stats.no | VALID | 2024 | 1.9 | 41 | 5.50 |
| 18 | D018 | Brazil | LATAM | brazil@ibge.br | VALID | 2024 | 2.2 | 35 | 2.16 |
| 19 | D019 | Mexico | LATAM | mexico@@gov.mx | VALID | 2024 | 2.0 | 37 | 1.29 |
| 20 | D020 | France | EUROPE | france@gov.fr | VALID | 2024 | 1.9 | 40 | 0.68 |
| 21 | D021 | Uk | EUROPE | uk@stats.uk | VALID | 2024 | 2.3 | 42 | 0.68 |
| 22 | D022 | Australia | OCEANIA | aus@gov.au | VALID | 2024 | 2.1 | 39 | 0.26 |
| 23 | D023 | Egypt | AFRICA | egypt.gov | INVALID_EMAIL | 2024 | 2.5 | 36 | 1.09 |
| 24 | D024 | Turkey | EUROPE | turkey@stats.tr | VALID | 2024 | 2.2 | 38 | 0.85 |
5.PROC SORT NODUPKEY
proc sort data=divorce_clean out=dedup_data nodupkey;
by country_name report_year;
run;
proc print data=dedup_data;
run;
LOG:
OUTPUT:
| Obs | record_id | country_name | region_code | survey_email | status_flag | report_year | divorce_rate | median_age | population_m |
|---|---|---|---|---|---|---|---|---|---|
| 1 | D022 | Australia | OCEANIA | aus@gov.au | VALID | 2024 | 2.1 | 39 | 0.26 |
| 2 | D002 | Belarus | EUROPE | surveyagency.com | INVALID_EMAIL | 2024 | 4.1 | 41 | 9.20 |
| 3 | D009 | Belgium | EUROPE | contact@@belgium.be | VALID | 2024 | 2.8 | 37 | 11.70 |
| 4 | D018 | Brazil | LATAM | brazil@ibge.br | VALID | 2024 | 2.2 | 35 | 2.16 |
| 5 | D010 | Canada | NORTH_AMERICA | canada.ca | INVALID_EMAIL | 2024 | 2.1 | 40 | 0.39 |
| 6 | D006 | Denmark | EUROPE | denmark@survey.dk | VALID | 2024 | 3.1 | . | 5.90 |
| 7 | D023 | Egypt | AFRICA | egypt.gov | INVALID_EMAIL | 2024 | 2.5 | 36 | 1.09 |
| 8 | D020 | France | EUROPE | france@gov.fr | VALID | 2024 | 1.9 | 40 | 0.68 |
| 9 | D015 | Germany | EUROPE | germany@gov.de | VALID | 2024 | 1.8 | 44 | 0.84 |
| 10 | D013 | India | ASIA | india@gov.in | VALID | 2024 | 1.1 | 34 | 14.30 |
| 11 | D014 | Japan | ASIA | japan@stats.jp | VALID | 2024 | 1.7 | 43 | 1.25 |
| 12 | D008 | Latvia | EUROPE | INVALID_EMAIL | 2024 | 4.2 | 39 | 1.80 | |
| 13 | D001 | Maldives | ASIA | data@stats.org | VALID | 2024 | 3.8 | 36 | 0.52 |
| 14 | D019 | Mexico | LATAM | mexico@@gov.mx | VALID | 2024 | 2.0 | 37 | 1.29 |
| 15 | D017 | Norway | EUROPE | norway@stats.no | VALID | 2024 | 1.9 | 41 | 5.50 |
| 16 | D003 | Portugal | EUROPE | info@agency.org | VALID | 2024 | 2.5 | 38 | 10.40 |
| 17 | D011 | Russia | EUROPE | data@russia.ru | VALID | 2024 | 3.9 | 42 | . |
| 18 | D012 | Spain | EUROPE | info@spain.es | VALID | 2024 | 2.0 | 39 | 0.48 |
| 19 | D016 | Sweden | EUROPE | sweden.gov | INVALID_EMAIL | 2024 | 2.5 | 39 | 0.10 |
| 20 | D024 | Turkey | EUROPE | turkey@stats.tr | VALID | 2024 | 2.2 | 38 | 0.85 |
| 21 | D021 | Uk | EUROPE | uk@stats.uk | VALID | 2024 | 2.3 | 42 | 0.68 |
| 22 | D005 | Usa | NORTH_AMERICA | census@usa.gov | VALID | 2024 | 2.4 | . | 3.40 |
Removes
duplicate countries and ensures analytical integrity.
6.PROC FORMAT
proc format;
value divorcerisk low-1.5='Low'
1.5<-3='Moderate'
3<-high='High';
run;
LOG:
7.PROC SQL Solution
proc sql;
create table sql_summary as
select region_code,count(*) as countries,
mean(divorce_rate) as avg_rate
from dedup_data
group by region_code;
quit;
proc print data=sql_summary;
run;
OUTPUT:
| Obs | region_code | countries | avg_rate |
|---|---|---|---|
| 1 | AFRICA | 1 | 2.50000 |
| 2 | ASIA | 3 | 2.20000 |
| 3 | EUROPE | 13 | 2.70769 |
| 4 | LATAM | 2 | 2.10000 |
| 5 | NORTH_AMERICA | 2 | 2.25000 |
| 6 | OCEANIA | 1 | 2.10000 |
8.DATA Step Equivalent
proc summary data=dedup_data nway;
class region_code;
var divorce_rate;
output out=summary_ds mean=avg_rate n=countries;
run;
proc print data=summary_ds;
run;
OUTPUT:
| Obs | region_code | _TYPE_ | _FREQ_ | avg_rate | countries |
|---|---|---|---|---|---|
| 1 | AFRICA | 1 | 1 | 2.50000 | 1 |
| 2 | ASIA | 1 | 3 | 2.20000 | 3 |
| 3 | EUROPE | 1 | 13 | 2.70769 | 13 |
| 4 | LATAM | 1 | 2 | 2.10000 | 2 |
| 5 | NORTH_AMERICA | 1 | 2 | 2.25000 | 2 |
| 6 | OCEANIA | 1 | 1 | 2.10000 | 1 |
9.PROC TRANSPOSE
proc transpose data=summary_ds out=transpose_summary;
by region_code;
var avg_rate;
run;
proc print data=transpose_summary;
run;
OUTPUT:
| Obs | region_code | _NAME_ | COL1 |
|---|---|---|---|
| 1 | AFRICA | avg_rate | 2.50000 |
| 2 | ASIA | avg_rate | 2.20000 |
| 3 | EUROPE | avg_rate | 2.70769 |
| 4 | LATAM | avg_rate | 2.10000 |
| 5 | NORTH_AMERICA | avg_rate | 2.25000 |
| 6 | OCEANIA | avg_rate | 2.10000 |
10.PROC REPORT
proc report data=divorce_quality nowd;
columns region_code country_name divorce_rate;
define region_code / group;
define country_name / display;
define divorce_rate / analysis mean format=divorcerisk.;
run;
OUTPUT:
| region_code | country_name | divorce_rate |
|---|---|---|
| AFRICA | Egypt | Moderate |
| ASIA | Maldives | High |
| Maldives | High | |
| India | Low | |
| Japan | Moderate | |
| EUROPE | Belarus | High |
| Portugal | Moderate | |
| Portugal | Moderate | |
| Denmark | High | |
| Latvia | High | |
| Belgium | Moderate | |
| Russia | High | |
| Spain | Moderate | |
| Germany | Moderate | |
| Sweden | Moderate | |
| Norway | Moderate | |
| France | Moderate | |
| Uk | Moderate | |
| Turkey | Moderate | |
| LATAM | Brazil | Moderate |
| Mexico | Moderate | |
| NORTH_AMERICA | Usa | Moderate |
| Canada | Moderate | |
| OCEANIA | Australia | Moderate |
11. SAS Macro Standardization
%macro validate(ds,variable,min,max);
data validate;
set &ds;
if &variable < &min then &variable=.;
if &variable > &max then &variable=.;
run;
proc print data=validate;
run;
%mend;
%validate(divorce_quality,median_age,18,100);
OUTPUT:
| Obs | risk_level | record_id | country_name | region_code | survey_email | status_flag | report_year | divorce_rate | median_age | population_m |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | MEDIUM | D001 | Maldives | ASIA | data@stats.org | VALID | 2024 | 3.8 | 36 | 0.52 |
| 2 | HIGH | D002 | Belarus | EUROPE | surveyagency.com | INVALID_EMAIL | 2024 | 4.1 | 41 | 9.20 |
| 3 | MEDIUM | D003 | Portugal | EUROPE | info@agency.org | VALID | 2024 | 2.5 | 38 | 10.40 |
| 4 | MEDIUM | D004 | Portugal | EUROPE | info@agency.org | VALID | 2024 | 2.9 | 38 | 10.40 |
| 5 | MEDIUM | D005 | Usa | NORTH_AMERICA | census@usa.gov | VALID | 2024 | 2.4 | . | 3.40 |
| 6 | MEDIUM | D006 | Denmark | EUROPE | denmark@survey.dk | VALID | 2024 | 3.1 | . | 5.90 |
| 7 | MEDIUM | D007 | Maldives | ASIA | data@stats.org | VALID | 2024 | 3.8 | 36 | 0.52 |
| 8 | HIGH | D008 | Latvia | EUROPE | INVALID_EMAIL | 2024 | 4.2 | 39 | 1.80 | |
| 9 | MEDIUM | D009 | Belgium | EUROPE | contact@@belgium.be | VALID | 2024 | 2.8 | 37 | 11.70 |
| 10 | MEDIUM | D010 | Canada | NORTH_AMERICA | canada.ca | INVALID_EMAIL | 2024 | 2.1 | 40 | 0.39 |
| 11 | MEDIUM | D011 | Russia | EUROPE | data@russia.ru | VALID | 2024 | 3.9 | 42 | . |
| 12 | MEDIUM | D012 | Spain | EUROPE | info@spain.es | VALID | 2024 | 2.0 | 39 | 0.48 |
| 13 | LOW | D013 | India | ASIA | india@gov.in | VALID | 2024 | 1.1 | 34 | 14.30 |
| 14 | LOW | D014 | Japan | ASIA | japan@stats.jp | VALID | 2024 | 1.7 | 43 | 1.25 |
| 15 | LOW | D015 | Germany | EUROPE | germany@gov.de | VALID | 2024 | 1.8 | 44 | 0.84 |
| 16 | MEDIUM | D016 | Sweden | EUROPE | sweden.gov | INVALID_EMAIL | 2024 | 2.5 | 39 | 0.10 |
| 17 | LOW | D017 | Norway | EUROPE | norway@stats.no | VALID | 2024 | 1.9 | 41 | 5.50 |
| 18 | MEDIUM | D018 | Brazil | LATAM | brazil@ibge.br | VALID | 2024 | 2.2 | 35 | 2.16 |
| 19 | MEDIUM | D019 | Mexico | LATAM | mexico@@gov.mx | VALID | 2024 | 2.0 | 37 | 1.29 |
| 20 | LOW | D020 | France | EUROPE | france@gov.fr | VALID | 2024 | 1.9 | 40 | 0.68 |
| 21 | MEDIUM | D021 | Uk | EUROPE | uk@stats.uk | VALID | 2024 | 2.3 | 42 | 0.68 |
| 22 | MEDIUM | D022 | Australia | OCEANIA | aus@gov.au | VALID | 2024 | 2.1 | 39 | 0.26 |
| 23 | MEDIUM | D023 | Egypt | AFRICA | egypt.gov | INVALID_EMAIL | 2024 | 2.5 | 36 | 1.09 |
| 24 | MEDIUM | D024 | Turkey | EUROPE | turkey@stats.tr | VALID | 2024 | 2.2 | 38 | 0.85 |
Professional
reporting often relies on PROC REPORT because it provides greater presentation
control than PROC PRINT.
12.R Raw Dataset
library(tidyverse)
divorce_raw <- tibble(
record_id=c("D001","D002","D003"),
country_name=c("portugal","USA","INDIA"),
region_code=c("europe","NORTH_AMERICA","asia"),
divorce_rate=c(-2.5,2.4,1.1),
median_age=c(38,250,34),
survey_email=c("survey.com","usa@gov.gov","NULL")
)
OUTPUT:
|
record_id |
country_name |
region_code |
divorce_rate |
median_age |
survey_email |
|
D001 |
portugal |
europe |
-2.5 |
38 |
survey.com |
|
D002 |
USA |
NORTH_AMERICA |
2.4 |
250 |
usa@gov.gov |
|
D003 |
INDIA |
asia |
1.1 |
34 |
NULL |
13.R Cleaning Layer
library(dplyr)
library(stringr)
library(lubridate)
library(janitor)
clean_data <- divorce_raw %>%
mutate(
country_name=str_to_title(country_name),
region_code=str_to_upper(region_code),
divorce_rate=abs(divorce_rate),
median_age=if_else(median_age>100,
NA_real_,median_age),
survey_email=na_if(survey_email,"NULL"),
status=case_when(grepl("@",survey_email)~"VALID",
TRUE~"INVALID")
) %>%
distinct(country_name,.keep_all = TRUE)
OUTPUT:
|
record_id |
country_name |
region_code |
divorce_rate |
median_age |
survey_email |
status |
|
D001 |
Portugal |
EUROPE |
2.5 |
38 |
survey.com |
INVALID |
|
D002 |
Usa |
NORTH_AMERICA |
2.4 |
usa@gov.gov |
VALID |
|
|
D003 |
India |
ASIA |
1.1 |
34 |
INVALID |
SAS vs R Cleaning Logic
|
SAS |
R |
|
IF THEN
ELSE |
if_else() |
|
SELECT
WHEN |
case_when() |
|
PROC
SQL |
dplyr
joins |
|
STRIP |
str_trim() |
|
PROPCASE |
str_to_title() |
|
COALESCEC |
coalesce() |
|
COMPRESS |
str_replace_all() |
Enterprise Validation and
Compliance
In
regulated environments such as clinical research:
- SDTM datasets feed
submission packages.
- ADaM datasets support
statistical analyses.
- Traceability links raw data
to outputs.
- QC programmers independently
validate derivations.
- Audit trails document
transformations.
One major
SAS danger:
if age > . then
Since
missing numeric values are lower than valid values, improper logic can
accidentally include missing records.
This has
caused real submission issues in production environments.
Business Logic Behind
Cleaning
Missing
values are not simply empty spaces.
A missing
visit date can change survival analysis results.
An
incorrect age of 250 years may distort demographic distributions.
Negative
billing values may falsely indicate refunds.
Text
normalization prevents duplicate categories:
USA
usa
Usa
U.S.A
Without
normalization these become four countries.
Date
standardization allows correct longitudinal analysis.
Email
validation ensures communication workflows remain operational.
In
insurance analytics, incorrect claims dates alter fraud models.
In
retail, duplicate customer IDs inflate revenue estimates.
Data
cleaning protects analytical trust.
Twenty Enterprise Best
Practices
- Standardize metadata.
- Validate before
transformation.
- Separate QC programming.
- Maintain lineage.
- Document assumptions.
- Version macros.
- Use reusable formats.
- Log all warnings.
- Remove duplicates early.
- Validate joins.
- Protect identifiers.
- Audit missing values.
- Use defensive programming.
- Standardize labels.
- Validate ranges.
- Monitor truncation.
- Track derivations.
- Review outputs
independently.
- Automate QC checks.
- Preserve raw data
permanently.
Twenty One-Line Insights
- Dirty data creates expensive
business mistakes.
- Validation beats visual
inspection.
- Metadata drives
reproducibility.
- Standardization improves
trust.
- Missing values hide risk.
- Duplicate records distort
truth.
- Truncation silently destroys
information.
- Audit trails matter.
- QC independence protects
submissions.
- Clean inputs improve AI
outputs.
- Date quality affects
forecasting.
- Governance improves
analytics.
- Macros improve consistency.
- Reproducibility creates
confidence.
- Data lineage supports
compliance.
- Production code requires
validation.
- PROC REPORT enhances
communication.
- Tidy data improves modeling.
- Automation reduces human
error.
- Reliable data creates
reliable decisions.
SAS and R Comparison
|
Feature |
SAS |
R |
|
Auditability |
Excellent |
Moderate |
|
Regulatory
Acceptance |
Excellent |
Growing |
|
Scalability |
Excellent |
Excellent |
|
Visualization |
Moderate |
Excellent |
|
Flexibility |
Moderate |
Excellent |
|
Validation
Framework |
Strong |
Custom |
|
Metadata
Handling |
Excellent |
Good |
|
Reporting |
Excellent |
Excellent |
SAS
dominates highly regulated industries because of traceability and audit
readiness. R excels in exploratory analytics, machine learning, and
visualization.
Together
they provide one of the strongest enterprise analytics combinations available
today.
Conclusion
Modern
analytics platforms are not limited by algorithms; they are limited by data
quality.
Whether
studying global divorce trends, patient outcomes, insurance claims, retail transactions,
or banking risk scores, the same principle applies:
poor
input creates poor decisions.
SAS
provides industrial-grade governance, reproducibility, validation frameworks,
metadata control, and regulatory acceptance. R provides exceptional flexibility,
visualization capabilities, advanced modeling libraries, and rapid
experimentation.
Together
they form a powerful partnership for enterprise analytics.
The
future belongs not to organizations with the largest datasets, but to
organizations with the most trustworthy datasets.
Clean
data becomes reliable evidence.
Reliable
evidence becomes informed decisions.
Informed
decisions create business value.
That transformation from corrupted records to analytical intelligence is where professional SAS programmers and data scientists create their greatest impact.
Interview Questions and
Answers
1. How would you remove duplicate patient records?
Answer:
Use PROC SORT NODUPKEY or SQL DISTINCT in SAS and distinct() in R.
2. Why is LENGTH placement important?
Answer:
Character variables inherit length during compilation. Incorrect placement
causes irreversible truncation.
3. How does SAS treat missing numeric values?
Answer:
Missing values are smaller than valid numbers, which can create unexpected
filtering results.
4. When would PROC SQL outperform DATA Step?
Answer:
Complex joins and aggregations typically benefit from PROC SQL readability and
flexibility.
5. Why maintain raw datasets unchanged?
Answer:
Regulatory traceability and audit requirements demand preservation of original
source data.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
About the Author:
About the Author:
SAS Learning Hub is a data analytics and SAS programming platform focused on clinical, financial, and real-world data analysis. The content is created by professionals with academic training in Pharmaceutics and hands-on experience in Base SAS, PROC SQL, Macros, SDTM, and ADaM, providing practical and industry-relevant SAS learning resources.
Disclaimer:
The datasets and analysis in this article are created for educational and demonstration purposes only. Here we learn about DIVORCE DATA.
Our Mission:
This blog provides industry-focused SAS programming tutorials and analytics projects covering finance, healthcare, and technology.
This project is suitable for:
· Students learning SAS
· Data analysts building portfolios
· Professionals preparing for SAS interviews
· Bloggers writing about analytics and smart cities
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment