Hi everyone,
I've looked through these forums, and I know there are several other posts on this, but they don't do exactly what I need them to do, and I can't figure it out, so I thought someone might be able to help. I need to add leading 0s to a variable, say "var1" so the digits or characters add to 8 (123 would become 00000123) so then when I concatenate with another variable say "var2" which has a value of P or " " (blank but not missing), I end up with a variable that looks something like P00004444 with 9 characters. The issue is that some of my values for var1 are already 9 digits and that is the same row that has a "blank" var2. In my code, if I do "tostring" and change the format to %0.8 I get 8 leading 0s and can easily add those to var2 to get my P00004444. However, with my var1 that already has 9 digits, this actually converts to string in scientific notation to truncate it to 8 characters. This causes an issue when I export to CSV because excel does not recognize the actual number, it only recognizes the scientific notation, and the value becomes something like 11000000. The second issue is that when I export to CSV I lose all of those leading 0s.
My data looks something like this:
***Generate Leading 9 digits with leading 0s and PTIN***
tostring var1, gen(var1_str) format("%08.0f")
gen combined = var2+var1_str
gen var1_len = length(var1_str)
replace var1= "0" + var1 if var1_len == 8
This displays how I want it for the most part in Stata, with the exception for the scientific notations. However, the export to CSV drops and of the leading 0s if they don't have a P and the scientific notation is also problematic for the export.
Just to further clarify what I want to see is all values to be 9 characters. So if there is a 5 character var1 I should add 3 0s and add var2. But if there are 9 characters in var1 and there is a P in var2, I need to drop the 1st character and add P. This is why I made it %0.8. If var2 is blank var1 just needs to be 9 characters. Then I need to export this to CSV and keep the display format.
Any help would be much appreciated.
I've looked through these forums, and I know there are several other posts on this, but they don't do exactly what I need them to do, and I can't figure it out, so I thought someone might be able to help. I need to add leading 0s to a variable, say "var1" so the digits or characters add to 8 (123 would become 00000123) so then when I concatenate with another variable say "var2" which has a value of P or " " (blank but not missing), I end up with a variable that looks something like P00004444 with 9 characters. The issue is that some of my values for var1 are already 9 digits and that is the same row that has a "blank" var2. In my code, if I do "tostring" and change the format to %0.8 I get 8 leading 0s and can easily add those to var2 to get my P00004444. However, with my var1 that already has 9 digits, this actually converts to string in scientific notation to truncate it to 8 characters. This causes an issue when I export to CSV because excel does not recognize the actual number, it only recognizes the scientific notation, and the value becomes something like 11000000. The second issue is that when I export to CSV I lose all of those leading 0s.
My data looks something like this:
2222 | p |
84375437 | p |
77 | p |
111111111 | p |
999999999 | |
99999 | p |
83687 | p |
666666669 | |
543264659 | |
766576 | 9 |
***Generate Leading 9 digits with leading 0s and PTIN***
tostring var1, gen(var1_str) format("%08.0f")
gen combined = var2+var1_str
gen var1_len = length(var1_str)
replace var1= "0" + var1 if var1_len == 8
This displays how I want it for the most part in Stata, with the exception for the scientific notations. However, the export to CSV drops and of the leading 0s if they don't have a P and the scientific notation is also problematic for the export.
Just to further clarify what I want to see is all values to be 9 characters. So if there is a 5 character var1 I should add 3 0s and add var2. But if there are 9 characters in var1 and there is a P in var2, I need to drop the 1st character and add P. This is why I made it %0.8. If var2 is blank var1 just needs to be 9 characters. Then I need to export this to CSV and keep the display format.
Any help would be much appreciated.
Comment