Benedict Anderson in his Imagined Communities puts forward a theory for the breakup of the former Spanish colonies.
Creoles, Spanish born in the new world, functionaries basically only served in Mexico or their 'native' territory. For example an Argentinean bureaucrat could work in Argentina or Mexico but not Peru or Chile.
The different territories were very far apart, making travel difficult and time consuming.
Newspapers reported on events in such a way that local events were seen as part of events in that territory, which became an imagined community of similar people, while events in other regions were seen as similar, but not the same.
This is true today, think about how Canadians and Americans react to something happening in Windsor OR Detroit involving people they have never met.
Thus the elites were not gaining the idea of a United States of South America.
Anderson states that:
the 'failure' of the Spanish-American experience to generate permanent Spanish-American-wide nationalism reflects both the general level of development of capitalism and technology ... [and their] backwardness ... in relation to the administrative stretch of the empire. (pp63) I added the [and their]
I know less about Brazil but I would imagine it has to do with the King moving there and with the main population centres being on the coast and thus easy to travel between by boat compared to say Buenos Aires to Santiago which these days is a lovely bus ride but in colonial times was a long journey.