m****r 发帖数: 202 | 1 Hi,
I have a file, total 5000 obs,
a1 b1
1 cigatette 3 beers
3 cigar 3 glasses
1 cigar 1 beer
1/4 pack 4 drinks
10 ciggars 1 beer
2 cigarettes 1 drink
3 cig 2 cans
. .
. .
. .
. .
. .
I need to figure out the frequency of the two observations, so I have to
recode these two to num since they are char.Could you please help me to do? | s*********r 发帖数: 909 | 2 I am confused.We usually do frequency on categorical variables. why do you
need change char to num?
【在 m****r 的大作中提到】 : Hi, : I have a file, total 5000 obs, : a1 b1 : 1 cigatette 3 beers : 3 cigar 3 glasses : 1 cigar 1 beer : 1/4 pack 4 drinks : 10 ciggars 1 beer : 2 cigarettes 1 drink : 3 cig 2 cans
| m****r 发帖数: 202 | 3 Sorry, maybe I should say I need to analyze these two observations. I want
to know the average cigar/beer be taken. My supervisor told me recode them
to num first.
Thank you. | h********o 发帖数: 103 | 4 Is this what you want?
===============================
DATA OLD;
INPUT A1 & $12. B1 & $9.;
CARDS;
1 cigatette 3 beers
3 cigar 3 glasses
1 cigar 1 beer
1/4 pack 4 drinks
10 ciggars 1 beer
2 cigarettes 1 drink
3 cig 2 cans
;
DATA NEW;
SET OLD;
A1_SUB1 = SCAN(A1,1,"");
A1_SUB2 = SCAN(A1,2,"");
B1_SUB1 = SCAN(B1,1,"");
B1_SUB2 = SCAN(B1,2,"");
IF A1_SUB1 = "1/4" THEN A1_SUB1 = "0.25";
A1_CHAR = A1_SUB2;
A1_NUM = INPUT(A1_SUB1,BEST.);
B1_CHAR = B1_SUB2;
B1_NUM = INPUT(B1_SUB1,BEST.);
X = A1_NUM + B1_NUM;
KEEP A1_CHAR A1_NUM B1_CHAR B1_NUM;
RUN;
PROC PRINT DATA = NEW NOOBS;
RUN;
=========================================================
A1_CHAR A1_NUM B1_CHAR B1_NUM
cigatette 1.00 beers 3
cigar 3.00 glasses 3
cigar 1.00 beer 1
pack 0.25 drinks 4
ciggars 10.00 beer 1
cigarettes 2.00 drink 1
cig 3.00 cans 2 | m****r 发帖数: 202 | 5 Thank you honglajiao. You did do me a great favor. | k*******a 发帖数: 772 | 6 if there is 1/3 or 1/5...then it does not work
【在 h********o 的大作中提到】 : Is this what you want? : =============================== : DATA OLD; : INPUT A1 & $12. B1 & $9.; : CARDS; : 1 cigatette 3 beers : 3 cigar 3 glasses : 1 cigar 1 beer : 1/4 pack 4 drinks : 10 ciggars 1 beer
| h********o 发帖数: 103 | 7 if there is 1/3 or 1/5, this one should work...
==================================================
DATA OLD;
INPUT A1 & $12. B1 & $9.;
CARDS;
1 cigatette 3 beers
3 cigar 3 glasses
1 cigar 1 beer
1/4 pack 4 drinks
10 ciggars 1 beer
2 cigarettes 1 drink
3 cig 2 cans
1/5 pack 1/10 bot
;
DATA NEW;
SET OLD;
A1_SUB1 = SCAN(A1,1);
A1_SUB2 = SCAN(A1,2);
A1_SUB3 = SCAN(A1,3);
B1_SUB1 = SCAN(B1,1);
B1_SUB2 = SCAN(B1,2);
B1_SUB3 = SCAN(B1,3);
IF MISSING(A1_SUB3) THEN DO;
A1_CHAR = A1_SUB2;
A1_NUM = INPUT(A1_SUB1,BEST.);
END;
ELSE DO;
A1_CHAR = A1_SUB3;
A1_NUM = INPUT(A1_SUB1,BEST.) / INPUT(A1_SUB2,BEST.);
END;
IF MISSING(B1_SUB3) THEN DO;
B1_CHAR = B1_SUB2;
B1_NUM = INPUT(B1_SUB1,BEST.);
END;
ELSE DO;
B1_CHAR = B1_SUB3;
B1_NUM = INPUT(B1_SUB1,BEST.) / INPUT(B1_SUB2,BEST.);
END;
KEEP A1_CHAR A1_NUM B1_CHAR B1_NUM;
RUN;
PROC PRINT DATA = NEW NOOBS;
RUN;
===============================================
A1_CHAR A1_NUM B1_CHAR B1_NUM
cigatette 1.00 beers 3.0
cigar 3.00 glasses 3.0
cigar 1.00 beer 1.0
pack 0.25 drinks 4.0
ciggars 10.00 beer 1.0
cigarettes 2.00 drink 1.0
cig 3.00 cans 2.0
pack 0.20 bot 0.1 | m****r 发帖数: 202 | 8 this file has 20 data sets, and every data set has 200-5000 observations, so
I write the following according to honglajiao's help
%macro recode(dr=,category=,amount=,else=);
proc sql noprint;
create table &dr as
select &dr
from .....;
quit;
data new;
set &dr;
if scan(&dr,2," ")="&category" then &dr._re=1*substr(&dr,1,2);
else if scan(&dr,2," ")="&amount" then &dr._re=6*substr(&dr,1,2);
else if &dr="1/2" then &dr._re=0.5;
else &dr._re=1*substr(&dr,1,2);
run;
proc print;run;
%mend;
options symbolgen mprint;
%recode(dr=b1,category=beer,amount=pack)
this is the first step filter.
Thanks a lot to honglajiao |
|