BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bedi
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
snoopy369
Barite | Level 11

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).

View solution in original post

2 REPLIES 2
snoopy369
Barite | Level 11

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).

Bedi
Calcite | Level 5

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.

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1181 views
  • 0 likes
  • 2 in conversation