Announcement

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

  • reshape panel data for graphs

    Hi,

    I have subscribers per market (circle) for firms over time. There are two telecom company - BSNL and BSNL_VMOs, the circles/markets are "Andhra Pradhesh, ""Assam",.. and the time is measured at month-year level in wide format. I wanted to reshape the data to long format for plotting graphs (line charts representing each circle on a single graph with time (month-year) on X axis).

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str26 Circle long(BSNL_jan_2019 BSNL_VNOs_jan_2019 BSNL_feb_2019 BSNL_VNOs_feb_2019 BSNL_jan_2021 BSNL_VNOs_jan_2021 BSNL_feb_2021 BSNL_VNOs_feb_2021)
    "Andhra Pradesh"             10176735     10 10147764     11  9670048     12  9662168     13
    "Assam"                       2418559     20  2502807     12  2981556     34  3009116     23
    "Bihar"                       5092654     30  5284522     12  5969667     23  5882003     23
    end
    
    reshape long BSNL_ BSNL_VMOs_    , i(Circle) j(month_year)
    variable month_year contains all missing values
    r(498);

    I get the error above because the circle name variable contains month_year. Is there any way to use the reshape command here?
    Last edited by Arvind Sharma; 28 Aug 2022, 12:28.

  • #2
    Code:
    reshape long BSNL BSNL_VNOs ,i(Circle) string
    gen month = mofd(date(substr(_j,6,4) + substr(_j,2,3), "YM"))

    Comment


    • #3
      Your reshape command would work with two fixes: BSNL_VNOs_ instead of BSNL_VMOs_, and adding the option string:
      Code:
      reshape long BSNL_ BSNL_VNOs_    , i(Circle) j(month_year) string

      #2 goes one step further and creates a variable that Stata can use for date operations. If you use that solution, you might want to change its display format to be more human-readable:
      Code:
      format %tm month

      Comment


      • #4
        Here's another way to handle the dates

        Code:
        reshape long BSNL_ BSNL_VNOs_, i(Circle) j(month_year) string 
        gen mdate = monthly(month_year, "MY") 
        format mdate %tm

        Comment


        • #5
          Thank you @Snilsberg - the string command made the reshape function work.

          Comment


          • #6
            Terminology: string here is an option, not a command.

            reshape is a command, not a function.

            Comment

            Working...
            X