Dear All,
I have encountered a strange behavior of the colnames extended macro, which doesn't seem to respect the spaces included in the matrix colnames (I didn't test, but I believe the situation with the rownames would be the same).
My expectation was that the colnames extended macro will return the contents of the colnames structure with quotes introduced as necessary to envelop the content containing spaces, but there is nothing in the returned content that would allow me to correctly parse and attribute the parts to different columns of the matrix (in the presence of spaces).
Since the built-in command shows the colnames structure correctly, it means it is not a storage problem, but the extended macro's problem. So while looking for a workaround I came up with the following 2 solutions: with and without the use of Mata:
Both seem to work fine, but I don't see the need for such acrobatics to extract the colnames/rownames.
Hope this would be useful for someone who bumps into the same problem. Should there be a better way, please let me know.
PS: coincidentally this was the (long forgotten) question I've asked ~10 years ago, which didn't get any responses at that time. Now both the cause of the problem and the workarounds are clear. It is also a good indication that this is unlikely to be fixed/changed at all. π
Best, Sergiy Radyakin
I have encountered a strange behavior of the colnames extended macro, which doesn't seem to respect the spaces included in the matrix colnames (I didn't test, but I believe the situation with the rownames would be the same).
Code:
matrix drop _all // create example matrix A: matrix A=0,0 matrix colnames A="Adult males" "Adult females" matrix list A // shows colnames correctly display `"`: colnames A'"' display `"`:word 1 of `: colnames A''"' // problem
Since the built-in command shows the colnames structure correctly, it means it is not a storage problem, but the extended macro's problem. So while looking for a workaround I came up with the following 2 solutions: with and without the use of Mata:
Code:
// Solution without Mata:
tempname tmpM
matrix `tmpM'=A[....,1]
local l1 `"`: colnames `tmpM''"'
matrix `tmpM'=A[....,2]
local l2 `"`: colnames `tmpM''"'
display `"`l1'"'
display `"`l2'"'
Code:
// Solution with Mata:
mata st_local("l1m",st_matrixcolstripe("A")[1,2])
display `"`l1m'"'
mata st_local("l2m",st_matrixcolstripe("A")[2,2])
display `"`l2m'"'
Hope this would be useful for someone who bumps into the same problem. Should there be a better way, please let me know.
PS: coincidentally this was the (long forgotten) question I've asked ~10 years ago, which didn't get any responses at that time. Now both the cause of the problem and the workarounds are clear. It is also a good indication that this is unlikely to be fixed/changed at all. π
Best, Sergiy Radyakin
Comment