Announcement

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

  • Panel data set - adding repeat responses

    Hi everyone, I have a panel dataset from a monthly survey with many repeat respondents that are identified through a variable called UNIQUE_FIRM_REFERENCE and a MONTHLY variable that indicates which month the survey was from. My problem is that for one of the questions (the variable is FOREIGNSALES_YN), the respondent's answers are only recorded the first time the respondent answered the survey. They other observations are noted as N/A. I would like to take the answer from the first observation and apply it to the rest of the responses with the same UNIQUE_FIRM_REFERENCE
    .
    My dataset is below. As an example, for the unique firm reference S35495785 observations, I want the FOREIGNSALES_YN variables to all be "No". Same for the firm S91060053.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str11 UNIQUE_FIRM_REFERENCE str119 FIRM_NAME long YEAR_MONTH str3 FOREIGNSALES_YN
    "S77422335" "0812805 B.C. Ltd."                   202301 "No" 
    "S35495785" "1 Hour Signs"                        202301 "No" 
    "S35495785" "1 Hour Signs"                        202302 "N/A"
    "S35495785" "1 Hour Signs"                        202303 "N/A"
    "S35495785" "1 Hour Signs"                        202304 "N/A"
    "S68292422" "1-800-Got-Junk? Vancouver Metro"     202211 "N/A"
    "S68292422" "1-800-Got-Junk? Vancouver Metro"     202212 "N/A"
    "S91060053" "1. Abundance Bakery 2. Jovic Bakery" 202301 "No" 
    "S91060053" "1. Abundance Bakery 2. Jovic Bakery" 202302 "N/A"
    "S91060053" "1. Abundance Bakery 2. Jovic Bakery" 202303 "N/A"
    end
    Thanks,
    Mathieu

  • #2
    Code:
    gen mdate = ym(floor(YEAR_MONTH/100), mod(YEAR_MONTH, 100))
    assert missing(mdate) == missing(YEAR_MONTH)
    format mdate %tm
    drop YEAR_MONTH
    
    by UNIQUE_FIRM_REFERENCE (mdate), sort: replace FOREIGNSALES_YN ///
        = FOREIGNSALES_YN[1]
    The first four lines of code convert your YEAR_MONTH variable to a Stata internal format monthly date variable. This is not strictly necessary for the present purpose--you could replace mdate by YEAR_MONTH in the final command and it would work properly because the sort-order with your original way of coding the dates agrees with chronological sort order. But for other purposes, your date variable will prove cumbersome or obstruct your work entirely. It is usually best to convert date variables to Stata internal format date variables (if they don't come that way to start with) early in your data management process.

    Comment

    Working...
    X