EVER BREAST-FED ON SAS [message #14742] |
Thu, 03 May 2018 06:45 |
Mlue
Messages: 92 Registered: February 2017 Location: North West
|
Senior Member |
|
|
Hi,
I've been working on translating this code from Stata to SAS, and I finally figured it out. I just thought I'd share for those who may be struggling as well.
This SAS code derives the results on "Table 11.2 Initial breastfeeding - Percentage ever breastfed" for Pakistan DHS 2012-13...
/** PAKISTAN DHS 2012-13 **/
LIBNAME DATASETS "\\STUDENT\Users\Mlue\Desktop\2018 DOCS\MAY 2018\PAKISTAN"; /* LOCATE DATASET FOLDER */
DATA PAKISTAN_EVERBF;
SET DATASETS.PKKR61FL;
WEIGHT = (V005 / 1000000);
psu = V021;
strata = V023;
/** CHILD'S AGE IN MONTHS **/
child_age=(V008-B3);
/** ONLY CHILDREN AGED LESS THAN TWO YEARS OLD **/
IF child_age < 24;
RUN;
/** FINDING THE YOUNGEST CHILD USING BIDX **/
PROC SORT DATA=WORK.PAKISTAN_EVERBF;
BY V001 V002 V003;
RUN;
PROC SQL;
TITLE 'MINIMUM BIDX OR YOUNGEST CHILD';
CREATE TABLE WORK.BIDX_QUERY as
SELECT V001,V002,V003,BIDX,M4,M38, MIN(BIDX) as MINBIDX format=comma16.
FROM WORK.PAKISTAN_EVERBF
/*WHERE "BIDX"=(V001 AND V002 AND V003)*/
group by V001,V002,V003
order by V001,V002,V003;
RUN;
/* MERGING */
PROC SORT DATA=work.BIDX_QUERY;
BY V001 V002 V003;
RUN;
PROC SORT DATA=work.PAKISTAN_EVERBF;
BY V001 V002 V003;
RUN;
/** MERGING/COMBINING THE DATA FILES **/
DATA PAKISTAN_MERGED;
MERGE work.PAKISTAN_EVERBF (in=x) work.BIDX_QUERY (in=y);
BY V001 V002 V003;
IF X AND Y;
RUN;
/*********************************************************************************/
/** START HERE **/
DATA PAKISTAN_FINAL_EVERBF;
SET WORK.PAKISTAN_MERGED;
IF (BIDX <= MINBIDX);
FORMAT place_delivery$18. ever_breastfed$6.;
/** CHILD AGE GROUPS **/
IF child_age in(0:1) THEN child_age_grp = "00-01";
IF child_age in(2:3) THEN child_age_grp = "02-03";
IF child_age in(4:5) THEN child_age_grp = "04-05";
IF child_age in(6:8) THEN child_age_grp = "06-08";
IF child_age in(9:11) THEN child_age_grp = "09-11";
IF child_age in(12:17) THEN child_age_grp = "12-17";
IF child_age in(18:23) THEN child_age_grp = "18-23";
/** FOR TABLE 11.2 - PERCENTAGE EVER BREASTFED **/
IF M4 in(93,95) THEN ever_breastfed="1. Yes";
IF M4 not in(93,95) THEN ever_breastfed="0. No";
/** DELIVERY CARE **/
IF M15 in(21:36) THEN place_delivery="1. Health facility";
IF M15 in(11:12) THEN place_delivery="2. At home";
IF M15 not in(11:12,21:36) THEN place_delivery="3. Other/Missing";
RUN;
PROC FREQ DATA=WORK.PAKISTAN_FINAL_EVERBF;
WEIGHT WEIGHT;
TITLE "TABLE 11.2 - N";
TABLE B4 * ever_breastfed /NOCOL NOROW NOPERCENT;
TABLE place_delivery * ever_breastfed /NOCOL NOROW NOPERCENT;
TABLE V025 * ever_breastfed /NOCOL NOROW NOPERCENT;
TABLE V024 * ever_breastfed /NOCOL NOROW NOPERCENT;
TABLE V106 * ever_breastfed /NOCOL NOROW NOPERCENT;
TABLE V190 * ever_breastfed /NOCOL NOROW NOPERCENT;
RUN;
PROC FREQ DATA=WORK.PAKISTAN_FINAL_EVERBF;
WEIGHT WEIGHT;
TITLE "TABLE 11.2 - %";
TABLE B4 * ever_breastfed /NOCOL NOFREQ NOPERCENT;
TABLE place_delivery * ever_breastfed /NOCOL NOFREQ NOPERCENT;
TABLE V025 * ever_breastfed /NOCOL NOFREQ NOPERCENT;
TABLE V024 * ever_breastfed /NOCOL NOFREQ NOPERCENT;
TABLE V106 * ever_breastfed /NOCOL NOFREQ NOPERCENT;
TABLE V190 * ever_breastfed /NOCOL NOFREQ NOPERCENT;
RUN;
/***********************************************************************************/
|
|
|