2017-07-13 1 views
1

SPSS/SPSS構文/ Excelを使用して、年齢データの列を年齢/月齢に標準化しようとしています。SPSSまたはExcelでの異種性年齢データの標準化

DO IF CHAR.INDEX(Age, "y")>1... for years 
DO IF CHAR.INDEX(Age, "m")>1... for months 
DO IF CHAR.INDEX(Age, "d")>1... for days 

と番号(複数可)は、直ちに年/ヶ月/日の量として、文字列の前にプログラムの参照を持っているとの総に追加:私の直感ではない場合、一連のすなわちループを使用することです後で年に変換できる日数(最小単位)になる可能性のある新しい変数です。

たとえば、セル「3 yr 5 mo」の場合、新しい変数(「DaysOld」など)に3 * 365 + 5 * 30.5 = 1248日を追加します。

セルの内容(年間を想定し任意の文字列なしの数字)の例:

2  
5 months  
11 days  
1.7  
13 yr  
22 yrs  
13 months  
10 mo  
6/19/2016  
3y10m  
10m  
12y  
3.5 years  
3 years  
11 mos  
1 year 10 months  
1 year, two months  
20 Y  
13 y/o  
3 years in 2014 

答えて

0

次の構文は、例えば(例多くを解決するため、間違いなくすべてではない彼らのだろう「1.7」または "。 2014年に3年」)。私はで動作するようにあなたのサンプルデータを再作成まず

...あなたはそれに多くの作業を行う必要がありますが、これはあなたがうまく始める必要があります。

data list list/age (a30). 
begin data 
"2" 
"5 months" 
"11 days" 
"1.7" 
"13 yr" 
"22 yrs" 
"13 Months" 
"10 mo" 
"6/19/2016" 
"3y10m" 
"10m" 
"12y" 
"3.5 years" 
"3 YEARS" 
"11 mos" 
"1 year 10 months" 
"1 year, two months" 
"20 Y" 
"13 y/o" 
"3 years in 2014" 
end data. 

仕事に:

* some necessary definitions. 

string ageCleaned (a30) chr (a1) nm d m y (a5). 
compute ageCleaned="". 

* my first step is to create a "cleaned" age variable (it's possible to 
    manage without this variable but using this is better for debugging and 
    improving the method). 
* in the `ageCleaned` variable I only keep digits, periods (for decimal 
    point) and the characters "d", "m", "y". 

do if CHAR.INDEX(lower(age),'ymd',1)>0. 
loop #chrN=1 to char.length(age). 
    compute chr=lower(char.substr(age,#chrN,1)). 
    if CHAR.INDEX(chr,'ymd.',1)>0 ageCleaned=concat(rtrim(ageCleaned),chr). 
end loop. 
end if. 

* the following line accounts for the word "days" which in the `ageCleaned` 
    variable has turned into the characters "dy". 

compute ageCleaned=replace(ageCleaned,"dy","d"). 
exe. 

* now I can work through the `ageCleaned` variable, accumulating digits 
    until I meet a character, then assigning the accumulated number to the 
    right variable according to that character ("d", "m" or "y"). 

compute nm="". 
loop #chrN=1 to char.length(ageCleaned). 
    compute chr=char.substr(ageCleaned,#chrN,1). 
    do if CHAR.INDEX(chr,'.',1)>0. 
     compute nm=concat(rtrim(nm),chr). 
    else. 
     if chr="y" y=nm. 
     if chr="m" m=nm. 
     if chr="d" d=nm. 
     compute nm="". 
    end if. 
end loop. 
exe. 

* we now have the numbers in string format, so after turning them into 
    numbers they are ready for use in calculations. 

alter type d m y (f8.2). 
compute DaysOld=sum(365*y, 30.5*m, d). 
+0

はい、本当にありがとうございました!残されたのは、数字だけのインスタンス、すなわち非常に簡単な「27」のインスタンスを転送することでした。あなたは私に1トンの時間を救った! – Rozo

関連する問題