Hi! I'm trying to count the unique number of string values for variable mainx, and x1-9. My dataset has an ID variable, and there are often duplicates of ID. Across ID, I want to know how many unique values there are for mainx and x1-9. Here's an example of some fake data. In this example, I would want unique to equal 3, because there are 3 unique values of x across the ID. The dataset is very large, and there are up to 100 duplicates for each ID, so looping through each observation isn't a great option. However, I could expand the data so that a duplicate is created where mainx equals each non-missing x1-9 for that ID, and then get the unique values for mainx across ID. Perhaps there's a more efficient way though.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str4 id str3(mainx x1 x2)
"2019" "ABC" "DEF" "GHI"
"2019" "ABC" "."   "."  
"2019" "DEF" "GHI" "ABC"
end