由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - [sas] how to recode these variables
相关主题
【包子】SAS 日期变量问题SAS里关于recode一问
关于recode data的问题,多谢。如何证明数据是伪造的?
SAS中FORMAT问题求教SPSS 和 R 的问题
SAS问题求助missing data question in R
也弱问一个SAS里面genotype/SNP variable recoding的问题请教一个SAS recode variable的问题吧
SAS 问题Help! A data step problem
复杂分类变量recoding?在R中ifelse如何运用于variable recoding?
SAS code question, special two do loopSAS CODE HELP!
相关话题的讨论汇总
话题: a1话题: b1话题: num话题: char话题: sub1
进入Statistics版参与讨论
1 (共1页)
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
1 (共1页)
进入Statistics版参与讨论
相关主题
SAS CODE HELP!也弱问一个SAS里面genotype/SNP variable recoding的问题
R question -- character variable recoding -- what's wrong???SAS 问题
Data Cleaning复杂分类变量recoding?
~回答一个同学们经常问的关于Python培训课的问题SAS code question, special two do loop
【包子】SAS 日期变量问题SAS里关于recode一问
关于recode data的问题,多谢。如何证明数据是伪造的?
SAS中FORMAT问题求教SPSS 和 R 的问题
SAS问题求助missing data question in R
相关话题的讨论汇总
话题: a1话题: b1话题: num话题: char话题: sub1