The MSBuildSDKsPath environment variable was set to .NET 7.0.402, and after deleting it, the error was resolved.
MSBuildSDKsPath