Announcement

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

  • How to replace values and destring variables

    I have a panel dataset. On the first row and first columns are variables which are names of 100 countries. This table is a bilateral dataset of the investment between countries.
    I set all names in the first row as variables. I want to reshape this dataset, from wide to long.
    Most variables are numeric but some are string because the string variables sometimes have C in values.
    I want to replace all these "C" values with 0. Then I want to make all string variables become numeric as the others.
    I guess I must use "loop". What can I do?

    My table looks like below (not exactly but same). The variables Argentina and Australia are string because they contain "C".
    Invested_country Albania Argentina Australia Austria
    Albania 142341 C C 29398
    Argentina 244323 912824 195335 89324
    Australia 857343 123122 C 15235
    Austria 423479 123123 209473 12345
    My target in the end is to reshape this table from wide to long, so I suppose to rename the variables as follows:
    Invested_country InvestorAlbania InvestorArgentina InvestorAustralia InvestorAustria
    Albania
    Argentina
    Australia
    Austria
    and then run the following code:

    HTML Code:
    generate id=_n
    reshape long Investor, i(id) j(ivstor,string)
    to get the table as below:
    Invested_country ivstor Investor
    Albania Albania 142341
    Argentina Albania 244323
    Australia Albania 857343
    Austria Albania 423479
    Albania Argentina 0
    Argentina Argentina 912824
    Australia Argentina 123122
    Austria Argentina 123123
    Albania Australia 0
    Argentina Australia 195335
    Australia Australia 0
    Austria Australia 209473
    Albania Austria 29398
    Argentina Austria 89324
    Australia Austria 15235
    Austria Austria 12345
    ... ... ...
    So in order to get my target, I must have variables in the same type first. That's why I ask the question above.

    Many thanks!

  • #2
    Please use dataex as suggested in discussion of your post at https://stackoverflow.com/questions/...with-same-type

    Comment


    • #3
      Thanks for your suggestion. I tried it and after that I can replace and destring the variables. But I had to name all variables that have "C", after the command "dataex". How can I run a loop to trace all variables that are string type (have C) and dataex them all at the same time?

      Comment


      • #4
        We don't need to see all variables. We just need to be clear what your problem is. So, just show say three variables with some observations with values "C".
        Last edited by Nick Cox; 22 May 2018, 05:23.

        Comment


        • #5
          Here below is the browse of my data. the variable "UnitedKingdom" is string type and has many "C" in values.
          Click image for larger version

Name:	C.png
Views:	1
Size:	65.2 KB
ID:	1445219

          Comment


          • #6
            Still not a dataex example, and we do advise against screenshots, but just about readable. Something like this may help.


            Code:
            ds, has(type string) 
            local varlist `r(varlist)' 
            foreach v of local varlist { 
                replace `v' = "0" if trim(`v') == "C" 
            }
            destring, replace

            Comment


            • #7
              Thank you very much. It works well now.

              Comment


              • #8
                Good. I suggest that you delete the Stack Overflow thread.

                Comment

                Working...
                X