Using a variable in page permalink

As I said, the permalink changes from YEAR_NAME to 2023, but the
link gets 404 error

Yes, and it’s because the post slug no longer matched the value in the database (in the wp_posts table, column post_name), hence the permalink URL became invalid and WordPress displayed a 404 error page.

So you need to add a rewrite rule for handling the year value which replaces the TOP_NAME in the slug.

Here’s an example you can try, and you can change the ^top_in_2023 with ^top_in_\d{4} instead so that it matches any 4-digit year:

add_action( 'init', function() {
        // for Pages, the query var is pagename
} );

Remember to flush the rewrite rules, by simply visiting the Permalink Settings admin page.