I had same issue and finally found a link to this project:
https://www.reddit.com/r/tasker/comments/8p0ywt/project_share_switching_to_saved_wifi/
The project itself does a bit more, but shows one way to get the saved wifis with default Tasker features.
So regarding your Q: But is there anyway to programmatically retrieve all saved network names and set it as a variable?
A: Yes, there is, using Tasker action of type Code -> Java Function (see also function $getConfiguredNetworks in the linked project):
1.) Use Code Java Function to get WifiManager from CONTEXT
2.) Use Code Java Function to get configured networks from WifiManager
3.-6.) Use Variable Search Replace to get SSIDs out of the configured networks using regular expression
7.-8.) Set variable to be checked by Tasker Wifi Near State
9.) (Optional) Trigger steps 1-8 automatically
Detailed Tasker actions:
1.) Java Function:
a) Class Or Object=CONTEXT
b) Function=getSystemService{Object}(String) <select using magnifying glass icon and
either scroll down or use filter on the bottom of the screen, pay attention to not pick the (Class) version>
c) Param=wifi
d) Return{Object}=(WifiManager) wifiman
--> result in Tasker view should look like this: (WifiManager) wifiman = CONTEXT.getSystemService(wifi)
2.) Java Function:
a) Class Or Object=wifiman
b) Function=getConfiguredNetworks{List}()
c) Return{List}=%get_configured_networks
--> result in Tasker view should look like this: %get_configured_networks = wifiman.getConfiguredNetworks()
Extract SSIDs, there are many ways, I did it looping over the list
3.) For: Variable %get_configured_networks Items %configured_network
Delete everyting from the variable that is not the SSID using regular expression
4.) Variable Search Replace:
a) Variable=%configured_network
b) Search=(?:.|\n)*SSID:\s*"(.*)"(?:.|\n)* <this regex will capture the SSID name in capture group 1>
c) Replace Matches=true
d) Replace with=$1
Add SSID to array
5.) Array Push: Variable Array %ssids Position 1 Value %configured_network
End loop
6.) End For
Join SSIDs with / as separator to fit Taskers wifi near syntax SSID1/SSID2/.../SSIDn
7.) Variable Join %ssids Joiner /
Set Global Variable to be checked by Taskers Wifi Near State
8.) Variable Set Name %SavedWifis To %ssids
So once this is done, a Wifi Near %SavedWifis can be used. But after you have added a new Wifi or removed an old one, you need to update the value again.
9.) Execute Taks to refresh %SavedWifis regularly (for example based on time event or whenever wifi connected), if anybody knows a way to react on a 'savedWifisUpdated' event, please let me know.