/setblock a command block with nearest player's name inside
Solution 1:
This is impossible. Minecraft doesn't have variables like strings. The best you can do is to output the command in chat, like this:
/tellraw @s [{"text":"/setblock ~ ~5 ~ chain_command_block 1 replace {Command:\"modcommand foo "},{"selector":"@p"},{"text":" DoThis\"}"}]
If you have the log output open, you can then copy the command into chat and run it. A modded client could also automatically execute it, but not a regular Vanilla client.