George M. Cohan wrote You're a Grand Old Flag in 1906. In the Library of Congress is found this account of the song's origin:
The original lyric for this perennial George M. Cohan favorite came, as Cohan later explained, from an encounter he had with a Civil War veteran who fought at Gettysburg. The two men found themselves next to each other and Cohan noticed the vet held a carefully folded but ragged old flag. The man reportedly then turned to Cohan and said, 'She's a grand old rag.' Cohan thought it was a great line and originally named his tune You're a Grand Old Rag. So many groups and individuals objected to calling the flag a rag, however, that he 'gave 'em what they wanted' and switched words, renaming the song You're a Grand Old Flag.