Announcement

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

  • values changes to date format when a stata file is exported to csv file

    Hi everyone,
    I have a big dataset including the age group variable. The variable contains "0-4", "5-9" etc. After exporting the dataset to a csv file, the "5-9" changes to a data format such as 9-May, 14-Oct, etc. Is there any way to avoid this conversion?
    Best,
    Nader

  • #2
    The variable contains "0-4", "5-9" etc. After exporting the dataset to a csv file, the "5-9" changes to a data format such as 9-May, 14-Oct, etc. Is there any way to avoid this conversion?
    I'll bet that isn't true. I'll bet if you open the .csv file in a plain text editor, not a spreadsheet application, you will see that it still says 5-9, 14-15, etc. The conversion to date format is being done by the spreadsheet program, not by Stata. There is nothing you can do in Stata that will prevent it. You have to reformat those cells within the spreadsheet. If that isn't practical, you could change the Stata variables from 5-9 to "5 through 9" (-help subinstr()-) and then export them to CSV. Your spreadsheet won't mistake those for dates.

    Comment


    • #3
      Thanks Clyde for your response. You are right, when I opened it in a text editor, it is fine. However, when the data is imported in R, the 5-9 age group goes after 45-49 age group. The problem is also a case in Stata. When the data is sorted by the age group variable, the 5-9 age group goes after 45-49. Obviously, the reason is that stata considers only the first character (4 not 45) when the variable is sorted. I appreciate your advice on this.
      Sorting in a good order does matter because I need the age groups to build an age pyramid, and because of the issue the age group of 5-9 is located after 45-49 age group in the pyramid.

      Comment


      • #4
        Originally posted by Mahdie Raj View Post
        Sorting in a good order does matter . . . because of the issue the age group of 5-9 is located after 45-49 age group in the pyramid.
        .ÿ
        .ÿversionÿ15.1

        .ÿ
        .ÿclearÿ*

        .ÿ
        .ÿinputÿstr10ÿage_grp

        ÿÿÿÿÿÿÿÿage_grp
        ÿÿ1.ÿ0-4
        ÿÿ2.ÿ5-9
        ÿÿ3.ÿ45-49
        ÿÿ4.ÿend

        .ÿ
        .ÿsortÿage_grp

        .ÿlist,ÿnoobs

        ÿÿ+---------+
        ÿÿ|ÿage_grpÿ|
        ÿÿ|---------|
        ÿÿ|ÿÿÿÿÿ0-4ÿ|
        ÿÿ|ÿÿÿ45-49ÿ|
        ÿÿ|ÿÿÿÿÿ5-9ÿ|
        ÿÿ+---------+

        .ÿ
        .ÿquietlyÿsplitÿage_grp,ÿgenerate(age_limit)ÿparse(-)ÿdestring

        .ÿ
        .ÿsortÿage_limit1

        .ÿ
        .ÿlist,ÿnoobsÿabbreviate(20)

        ÿÿ+-----------------------------------+
        ÿÿ|ÿage_grpÿÿÿage_limit1ÿÿÿage_limit2ÿ|
        ÿÿ|-----------------------------------|
        ÿÿ|ÿÿÿÿÿ0-4ÿÿÿÿÿÿÿÿÿÿÿÿ0ÿÿÿÿÿÿÿÿÿÿÿÿ4ÿ|
        ÿÿ|ÿÿÿÿÿ5-9ÿÿÿÿÿÿÿÿÿÿÿÿ5ÿÿÿÿÿÿÿÿÿÿÿÿ9ÿ|
        ÿÿ|ÿÿÿ45-49ÿÿÿÿÿÿÿÿÿÿÿ45ÿÿÿÿÿÿÿÿÿÿÿ49ÿ|
        ÿÿ+-----------------------------------+

        .ÿ
        .ÿexit

        endÿofÿdo-file


        .

        Comment


        • #5
          Another approach to the problem of the sort order is to change 5-9 to 05-09.

          Comment

          Working...
          X