BookmarkSubscribeRSS Feed
Ujjawal
Quartz | Level 8

I want to calculate distinct count by group. When i tried to create aggregate measure and use distinct count, it returns an error as variable is character (see the variable 'code' below)

 

ID flag Code
1 0 B1
1 0 B1
1 0 B2
1 1 A1
1 1 A1
1 1 A1
2 0 D1
2 0 D1
2 1 C1
2 1 C1

 

I want to generate the following output in cross tab. For ID =1, it should return this output (i.e. distinct count of variable 'Code'.

 

flag Status
0 2
1 1

 

I would put ID in the drop down.

1 REPLY 1
KachiM
Rhodochrosite | Level 12

Do you want a data step program?

 

You may presort the dataset by ID and flag. I used the grouped data by using NOTSORTED option in BY statement.

 

data want;
   do until(last.flag);
      set have;
      by id flag notsorted;
      if first.flag then do; pre_Code = Code; count = 1; end;
      else if pre_Code ^= Code then count + 1;
   end;
keep id flag count;
run;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Tips for filtering data sources in SAS Visual Analytics

See how to use one filter for multiple data sources by mapping your data from SAS’ Alexandria McCall.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 1137 views
  • 0 likes
  • 2 in conversation