Check for missing dataset earlier

When package is not found `pkg` is `None` and `pkg.id` causes an exception.