Dear statalist

I have a problem using margins on stata command.
I'm doing some research related to the effect of election on corporate investment.

I generated control variables called victory (which is defined as the numbers of vote that the winner got from the election. code: gen victory= max(voteparty1, voteparty2)) and difofvote (vote difference between the winner and the runner-up. For exmaple, if voteparty1> voteparty2, then the difofvote would be voteparty1-voteparty2)
Then I got two variables, 1. difofvote 2. victory.

I want to use the vote difference (difofvote ) between two major political parties to see if it is below the 25th percentile of victory margin distribution. But I'm not pretty sure what is margin distribution. I got this idea from previous studies.

Then I want to generate a dummy variable "Close" sets to one if difovote is below the 25th percentile of victory margin distribution (victorymargin). I'm new to use margins, so I'm not sure.

I found that I needed to conduct a regression before I could use the "margins" code, so I done a simple regression as follow:
reg difofvote victory, allbaselevels

After that, I wrote a code as follow:
margins, dydx(victory) at((p25) victory)

After I ran it on Stata, I found that I'm still not sure how to use this to create "Close" dummy.
So I asked my professor for help and he sent me codes as follow:

egen victorymargin_25=pctile(victorymargin), p(25)

gen Close=1 if victorymargin<=victorymargin_25 & victorymargin!=.
replace Close=0 if victorymargin>victorymargin_25 & victorymargin!=.


But they are not working and I don't think these codes are doing what I wanted to do.
So I think maybe I need to ask for some advice.
Can anyone here help me please.

Thank you!