Hi I have a sas data set of FMCG sector with below demographics
Males 45%
Females 55%
Age group 18-35 48%
Age group 35-55+ 52%
now the client wants me to calculate weighting factor for every respondent in the data so as to get below demographics percentage
Males 50%
Females 50%
Age group 18-35 30%
Age group 35-55+ 70%
If i calculate this through rim weights a respondents whould have weight factors as per below is
male - age 18-35 ---> 0.69444
male - age 35-35+ ---> 1.495
female - age 18-35 ---> 0.568
female - age 35-35+ ---> 1.223
Can i calculate such weight factors through SAS, where i can supply the expected precentages and get weight factors?
You can trivially calculate them by doing a PROC FREQ, then merge them back to the main table along with the targets.
data have;
call streaminit(7);
do _n_=1 to 2000;
if rand('uniform')<=.48 then age='18-35';
else age='35+';
if rand('uniform')<=.45 then sex='M ';
else sex='F';
output;
end;
run;
proc freq data=have;
tables age*sex/out=age_sex_totals;
run;
data targets;
length age $5 sex $1;
input age $ sex $ target;
datalines;
18-35 F .15
18-35 M .15
35+ F .35
35+ M .35
;;;;
run;
proc sort data=targets;
by age sex;
run;
data want_wts;
merge age_sex_totals targets;
by age sex;
weight = 100*target/percent;
run;
proc sort data=have;
by age sex;
run;
data want;
merge have want_wts;
by age sex;
run;
proc freq data=want;
weight weight;
tables age*sex;
run;
For more complicated cases, there are macros online (RAKINGE is the one I typically use).
You can trivially calculate them by doing a PROC FREQ, then merge them back to the main table along with the targets.
data have;
call streaminit(7);
do _n_=1 to 2000;
if rand('uniform')<=.48 then age='18-35';
else age='35+';
if rand('uniform')<=.45 then sex='M ';
else sex='F';
output;
end;
run;
proc freq data=have;
tables age*sex/out=age_sex_totals;
run;
data targets;
length age $5 sex $1;
input age $ sex $ target;
datalines;
18-35 F .15
18-35 M .15
35+ F .35
35+ M .35
;;;;
run;
proc sort data=targets;
by age sex;
run;
data want_wts;
merge age_sex_totals targets;
by age sex;
weight = 100*target/percent;
run;
proc sort data=have;
by age sex;
run;
data want;
merge have want_wts;
by age sex;
run;
proc freq data=want;
weight weight;
tables age*sex;
run;
For more complicated cases, there are macros online (RAKINGE is the one I typically use).
Hi Snoopy
RAKINGE is the macro i am interested but i cant understand where to enter the target weights and update the macro to run for age and gender .....can you please share a working e.g. for the same on a random data set similar to what i have explained earlier.
your guidance would be really helpful.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.