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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.