While product attribute is an individual characteristic of a product, product attribute set is a group of those characteristics that define a product. You will use product attributes every time when creating products in Magento. 

If you need to create multiple product attributes, doing this via the admin panel might require a lot of time. So in case, you need to do it fast, create an attribute set in Magento 2 programmatically. 

Note: before you create attribute sets you have to create product attributes. So you might need to have a look at our guide on how to create product attributes programmatically in Magento.

Use the following code to create an attribute set programmatically. Create the InstallData.php file in the app/code/Vendor/Module/Setup/InstallData.php.

<?php
namespace Vendor\Extension\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Setup\CategorySetupFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
class InstallData implements InstallDataInterface
{
private $attributeSetFactory;
private $attributeSet;
private $categorySetupFactory;
public function __construct(AttributeSetFactory $attributeSetFactory, CategorySetupFactory $categorySetupFactory )
{
$this->attributeSetFactory = $attributeSetFactory;
$this->categorySetupFactory = $categorySetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
$attributeSet = $this->attributeSetFactory->create();
$entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
$data = [
'attribute_set_name' => 'MyCustomAttribute',
'entity_type_id' => $entityTypeId,
'sort_order' => 200,
];
$attributeSet->setData($data);
$attributeSet->validate();
$attributeSet->save();
$attributeSet->initFromSkeleton($attributeSetId);
$attributeSet->save();
$setup->endSetup();
}
}

If you do everything correctly, you'll be able to see your custom attribute set among others when creating a new product:

Create attribute set in Magento 2 programmatically

Once you create product attribute sets and product attributes assign attributes to attribute sets programmatically.