Announcement

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

  • Difficulty in converting sas dataset to stata

    Hi everyone,

    I have bunch of sas datasets that I want to convert into stata format.

    For the nace sector codes starting with 0, I have the following problem:

    0892 (in sas)--> appear 892 in stata

    I first converted sas to csv then to stata. I guess I have to try something else...

    What is the way to obtain the same four digit code in stata?

    Thaks in advance,

    Naz


  • #2
    Naz:
    I assume that you haven't access to Stat/Trasfer.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Life is so much simpler for those who break down and buy Stat/Transfer. Academic institutions can often get reduced pricing. http://www.stattransfer.com/ . But also see http://www.ats.ucla.edu/stat/mult_pk...AS_toStata.htm
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 19.5 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://www3.nd.edu/~rwilliam

      Comment


      • #4
        What does the file look like in a text editor?

        If the leading 0's are gone in the raw file, then it's SAS that's dropping them, in which case, you need StatTransfer, or to explore options in SAS.

        However, I'm guessing that they are in the raw file, and Stata is dropping them. In which case, you could use the -infile- command to specify Stata read them as strings.

        Alternatively, if you know darn well exactly how wide these codes should be (always four digit), you can use the -import delimited- command then do something like the following:
        Code:
        tostring nace, generate(naces)
        replace naces="0" + naces if length(naces)<4 
        replace naces="0" + naces if length(naces)<4
        replace naces="0" + naces if length(naces)<4

        Comment


        • #5
          Oh, come to think of it, I think one of the export options in SAS is to save it as Stata. I don't have SAS handy to check, but have a look yourself.

          Comment


          • #6
            I agree with Ben's advice in #5, if your version of SAS supports saving Stata format datasets with PROC EXPORT. If not, then Ben's advice at #4 can be shortened somewhat as
            Code:
            tostring nace, generate(naces)
            replace naces = substr("0000"+trim(naces),-4,.)
            which might be more convenient if nace isn't the only variable that needs to be converted to a string with leading zeroes.

            Comment


            • #7
              Why yes, SAS can save as Stata. That should solve it nicely.

              That's so strange. I'd swear Richard's comment wasn't there before.
              Last edited by ben earnhart; 21 Jun 2015, 12:50.

              Comment


              • #8
                It's a bit cumbersome, but you can force Stata to recognize leading zeroes in string variables by using the stringcols() option of import delimited. I agree with others that having SAS save the files in Stata or SAS transport (Version 5) format would be easier in this case, as would using StatTransfer. But if you run across another instance of receiving delimited text files in the future (not necessarily from SAS), then you might want to keep that option in mind.

                .ÿversionÿ14.0

                .ÿ
                .ÿclearÿ*

                .ÿsetÿmoreÿoff

                .ÿ
                .ÿquietlyÿsetÿobsÿ2

                .ÿgenerateÿstr4ÿnaceÿ=ÿ"0892"

                .ÿquietlyÿreplaceÿnacÿ=ÿ"0893"ÿinÿ2

                .ÿ
                .ÿquietlyÿexportÿdelimitedÿusingÿNACE.csv

                .ÿtypeÿNACE.csv
                nace
                0892
                0893

                .ÿ
                .ÿ//ÿWrong
                .ÿquietlyÿimportÿdelimitedÿNACE.csv,ÿclearÿ

                .ÿlist,ÿnoobs

                ÿÿ+------+
                ÿÿ|ÿnaceÿ|
                ÿÿ|------|
                ÿÿ|ÿÿ892ÿ|
                ÿÿ|ÿÿ893ÿ|
                ÿÿ+------+

                .ÿ
                .ÿ//ÿRight
                .ÿquietlyÿimportÿdelimitedÿNACE.csv,ÿstringcols(1)ÿclearÿ

                .ÿlist,ÿnoobs

                ÿÿ+------+
                ÿÿ|ÿnaceÿ|
                ÿÿ|------|
                ÿÿ|ÿ0892ÿ|
                ÿÿ|ÿ0893ÿ|
                ÿÿ+------+

                .ÿ
                .ÿ*
                .ÿ*ÿEvenÿdoubleÿquotationÿmarksÿwon'tÿavoidÿtheÿneed
                .ÿ*
                .ÿquietlyÿexportÿdelimitedÿusingÿ"NACE.csv",ÿquoteÿreplace

                .ÿtypeÿNACE.csv
                nace
                "0892"
                "0893"

                .ÿ
                .ÿquietlyÿimportÿdelimitedÿf:\NACE.csv,ÿclearÿ

                .ÿlist,ÿnoobs

                ÿÿ+------+
                ÿÿ|ÿnaceÿ|
                ÿÿ|------|
                ÿÿ|ÿÿ892ÿ|
                ÿÿ|ÿÿ893ÿ|
                ÿÿ+------+

                .ÿ
                .ÿeraseÿNACE.csv

                .ÿ
                .ÿexit

                endÿofÿdo-file


                .

                Comment

                Working...
                X