Hi Folks:
I'd like to impute data where id1name='D'. I'd like to bring current n=21 to a total of n =120 keeping the frequency distribution of n by idname. I tried it manually by using proc freq on the 'n' and multipling 120 by the resulting percent from proc freq and allocated those numbers to idname. I will have to do the same thing for the rest of id1name such as 'C' too.
Could you please help accomplish this imputation task more efficiently than manual?
data have;
input n id1name $ idname $;
cards;
0 D Buk
4 D Dalseo
1 D Dalseong
1 D Dong
3 D Jung
8 D Nam
2 D Seo
2 D Suseong
5 C Hart
6 C Sous
;
data have;
input n id1name $ idname $;
cards;
0 D Buk
4 D Dalseo
1 D Dalseong
1 D Dong
3 D Jung
8 D Nam
2 D Seo
2 D Suseong
5 C Hart
6 C Sous
;
data new_values;
input id1name $ newBase;
cards;
D 120
C 50
;;;
run;
proc sort data=have;
by id1name;
run;
proc sort data=new_values;
by id1name;
run;
proc freq data=have noprint;
by id1name;
table idname / out=percents;
weight n;
run;
data want;
merge percents new_values;
by id1name;
newValue = round(newBase*percent/100, 1);
run;
data have;
input n id1name $ idname $;
cards;
0 D Buk
4 D Dalseo
1 D Dalseong
1 D Dong
3 D Jung
8 D Nam
2 D Seo
2 D Suseong
5 C Hart
6 C Sous
;
data new_values;
input id1name $ newBase;
cards;
D 120
C 50
;;;
run;
proc sort data=have;
by id1name;
run;
proc sort data=new_values;
by id1name;
run;
proc freq data=have noprint;
by id1name;
table idname / out=percents;
weight n;
run;
data want;
merge percents new_values;
by id1name;
newValue = round(newBase*percent/100, 1);
run;
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.