Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to convert this SPSS code to Stata?

    Dear all,

    I am trying to generate a date of birth using century month code (CMC) format. I found SPSS code from Unicef website (MICS data) that does what I want. To be honest, I am very new to SPSS so I can understand some commands and operators in SPSS and convert them to Stata such as compute equals generate command in Stata. But, I do not totally understand about if and trunc functions in the following SPSS code. Thus, I would appreciate it if someone who is familiar with both SPSS and Stata could help me out with this issue.

    In this code example, WM8Y is years of birth and WM8M is months of birth, WM9 is age in years, cmcdoiw is date of interview in CMC format, and values 9996 and 96 denote don't know.
    Code:
    * calculate the CMC date of birth and age.
    do if (WM8Y < 9996 and WM8M < 96).
    + compute wdob = (WM8Y - 1900)*12 + WM8M.
    else if (WM8Y < 9996).
    + compute aldob = cmcdoiw - WM9*12 - 11.
    + compute audob = cmcdoiw - WM9*12 - 0.
    + compute ldob = (WM8Y - 1900)*12 + 1.
    + compute udob = (WM8Y - 1900)*12 + 12.
    + if (ldob >= aldob & ldob <= audob) wdob = trunc(rv.uniform(ldob,audob)).
    + if (aldob >= ldob & aldob <= udob) wdob = trunc(rv.uniform(aldob,udob)).
    + if (ldob > audob | aldob > udob) wdob = trunc(rv.uniform(aldob,audob)).
    else.
    + compute aldob = cmcdoiw - WM9*12 - 11.
    + compute audob = cmcdoiw - WM9*12 - 0.
    + compute wdob = trunc(rv.uniform(aldob,audob)).
    end if.

  • #2
    CMC is just months since january 1900. In Stata monthly dates are stored in months since january 1960. So all you have to do is add 720 (60*12) to your date of birth variable and you have a Stata monthly date. You make it look right by adding the command format dob %tm , where dob is the variable you created.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Originally posted by Maarten Buis View Post
      CMC is just months since january 1900. In Stata monthly dates are stored in months since january 1960. So all you have to do is add 720 (60*12) to your date of birth variable and you have a Stata monthly date. You make it look right by adding the command format dob %tm , where dob is the variable you created.
      Dear Dr. Maarten,

      Thank you for your detailed explanations on CMC. I appreciate that. However, I think code in #1 could help me to deal with missing value in months of birth since in raw data there is a sizable portion of people who do not remember their months of birth (wm8m). My plan is that I first follow the code in #1 to generate dob variable, and then use this dob to retrieve missing information on months of birth. In my research, months of birth is an important element.

      Do you have any ideas on how to convert the code in #1 to Stata?

      Thank you.

      Comment


      • #4
        In another thread, Maarten showed how to use the floor() function to achieve the same thing TRUNC does in SPSS. Here is his example using the data posted in that thread:

        Code:
        clear
        input str8 id score
        "00002484"  2.833333
        "00002484"         3
        "00002484"         3
        "00002484"         3
        "00003861" 2.3333333
        "00003861" 2.3333333
        "00003861"      2.25
        "00003861"      2.75
        "00004010"  2.666667
        "00004010"         3
        "00004010"      2.75
        "00004010"         3
        "00005099" 2.1666667
        "00005099"       2.5
        "00005099"      1.75
        "00007848" 2.3333333
        "00007848" 2.3333333
        "00007848"      1.75
        "00007848"       2.5
        "00015005" 1.8333334
        "00015005"  2.833333
        "00015005"      1.25
        "00015005"         3
        "00020453" 2.1666667
        end
        generate score2 = floor(score*100)/100
        format score2 %5.2f
        list, clean
        And here is the same example using TRUNC in SPSS:

        Code:
        * SPSS code.
        DATA LIST LIST / ID(A8) score(F8.6).
        BEGIN DATA
        "00002484"  2.833333
        "00002484"         3
        "00002484"         3
        "00002484"         3
        "00003861" 2.3333333
        "00003861" 2.3333333
        "00003861"      2.25
        "00003861"      2.75
        "00004010"  2.666667
        "00004010"         3
        "00004010"      2.75
        "00004010"         3
        "00005099" 2.1666667
        "00005099"       2.5
        "00005099"      1.75
        "00007848" 2.3333333
        "00007848" 2.3333333
        "00007848"      1.75
        "00007848"       2.5
        "00015005" 1.8333334
        "00015005"  2.833333
        "00015005"      1.25
        "00015005"         3
        "00020453" 2.1666667
        END DATA.
        COMPUTE score2 = TRUNC(score, 0.01).
        FORMATS score2 (F5.2).
        LIST.
        Regarding the DO IF - ELSE IF - ELSE approach in the SPSS code, I don't think you'll find anything exactly equivalent to that in Stata code. You'll have to add an if qualifier to each -generate- (or -replace-) command. You could also take a look at cond(). See this Stata Journal article, for example.

        HTH.
        --
        Bruce Weaver
        Email: [email protected]
        Version: Stata/MP 18.5 (Windows)

        Comment

        Working...
        X