The following is an elementary implementation of what you're after:

\documentclass{article}
\newcommand{\set}[3]{%
\{ #1#2{1},
\ifnum\pdfstrcmp{#3}{3}=0
#1#2 2
\else
\ldots
\fi
, #1#2{#3} \}
}
\begin{document}
$\set{a}{^}{3}$
$\set{e}{_}{n}$
\end{document}
We condition on what is presented as argument #3 using \pdfstrcmp. \pdfstrcmp{<strA>}{<strB>} does a string comparison between <strA> and <strB>, returning -1 | 0 | 1 if <strA> < | = | > <strB> (lexicographically).
The only difference between the two sets is the way the second term in the sequence is being set, so those elements are set without consideration of the condition. No consideration is added for n=1 or n=2.
A supplementary interface using a single argument:

\documentclass{article}
\newcommand{\set}[1]{\setaux#1\relax}
\def\setaux#1#2#3\relax{%
\{ {#1}#2 1,
\ifnum\pdfstrcmp{#3}{3}=0
{#1}#2 2
\else
\ldots
\fi
, {#1}#2{#3} \}
}
\begin{document}
$\set{a^3}$
$\set{e_n}$
$\set{\alpha^{22}}$
\end{document}
\dotsor\ldotsprecisely. Plus may be an auxiliar definition to have easier input\def\set#1{\setaux#1\relax}and\def\setaux#1#2#3\relax{..}that way one can write\set{e_3}or\set{\alpha^22}and get all set rather than\set{\alpha}{^}{22}. – Manuel Jan 14 '18 at 17:44\set{\alpha^m}? – Jan 14 '18 at 17:55\setbut it is OK with\sett. What is the problem? – C.F.G May 28 '21 at 07:08.aux? – Werner May 31 '21 at 17:50.auxfile? No I define it inside the.texfile. So why it works for\sett? – C.F.G May 31 '21 at 17:53\show\setprovide in your.logjust before defining it? Perhaps\setis already defined by another package. – Werner May 31 '21 at 17:56