No. This isn't a function of the iPhone OS from Apple. If this isn't a short term need, there might be a jailbreak tweak to turn the OS into a fully functioning router / hotspot, but the hardware isn't designed to extend a network as your use case requires.
The wireless card in your iMac, which is what is responsible for handling Wifi connections, is only capable of one connection at a time, to explain it most simply. The card cannot connect to a Wifi signal (your iPhone's Hotspot feature) and broadcast a signal at the same time.
However, a solution to what you wish to achieve may be possible if you can use USB Tethering as opposed to Wifi Hotspot on iPhone A. Then your wireless card on your iMac would be free to broadcast a Wifi signal for internet sharing.
I have not used any iPhone OS version since 3.1.2, where it was not called "hotspot" anywhere and the settings for tethering were even in a different set of menus. However, a quick search seems to show that you can activate USB tethering by going into your Personal Hotspot on the phone (Settings > General > Cellular > Personal Hotspot) while USB and Bluetooth are turned off. Then turning on Personal Hotspot, and tapping "USB Only" when prompted. USB tethering is also supposedly turned on even if Wifi and/or Bluetooth are on, yet I personally prefer to not have them on, and especially to not have the phone making a hotspot if I'm only going to use USB for connectivity.
Be advised, the iTunes Helper process is required to be running to provide the network interface for iPhone USB Tethering. This requires iTunes to be installed. If the iPhone does not initially provide you a network connection on your iMac, install if necessary and open iTunes, and it should start the iTunes Helper process and also add it to your login items.
At this point, your iMac should have a connection through your iPhone via USB. You can enable Wifi Internet Sharing in System Preferences and you should be good to go. Internet Sharing on most Macs, and I'm pretty certain on every modern one, produces a network in Infrastructure Mode. Which means there should be no problems connecting to it, even for devices that don't support certain other modes such as ad-hoc. So your other iPhone (B) should work fine.
Best Answer
It can be found under Settings → Personal Hotspot. Password is also set there. Settings → Cellular, Personal Hotspot is also the same setting. When you turn it on, it asks if you want to turn on Wi-Fi and Bluetooth, or, USB only.
If USB is connected, one can turn on Personal Hotspot from Control Centre, it will automatically use USB to share cellular data. In Control Centre, holding down (force touch/ 3D Touch) the square block with Wi-Fi, Cellular data etc reveals two extra options of Airdrop in the bottom left and Personal hotspot in bottom right.
If USB is disconnected, Wi-Fi should remain in "on" state, but no new connections should be allowed, i.e. white state, not blue in control centre. Same for Bluetooth. This lets you share mobile data. But you cannot share iPhone's Wi-Fi connection using Wi-Fi or Bluetooth or USB.
At times, when connected to USB, iPhone can prompt for passcode for allowing computer connected to access data on the iPhone.
Keep in mind on windows PC you need to have iTunes installed for it to trigger to ask that permission to 'Trust the PC' on the phone when connected via USB.