BookmarkSubscribeRSS Feed
tjalleph
Calcite | Level 5

Hi all,

 

New to SAS and SAS VA, coming from other BI tools. In SAS VA I'm trying to do something that could be called dynamic and conditional distinct couting. 

 

A simplified sample of my data would like this:

IDCategoryTypeCoded_answer
1AX0
1BX0
1CX1
1DX0
1EX0
2AY0
2CY0
2DY0
3AX0
3BX0
3CX 
3DX2
4AX1
4BX0
4CX0

 

Then I'd want to a distinct couting of ID's, categorizing them on the basis of the maximum of Coded_answer in that group. 

It would need to do so for many different slices of the data. Resulting tables for this dataset would have to look like:

Resulting tables  
 max = 0 max = 1max = 2
Total121
    
Categoriesmax = 0 max = 1max = 2
Cat A310
Cat B300
Cat C210
Cat D201
Cat E100
    
Typemax = 0 max = 1max = 2
X111
Y100

 

What would we be way to achieve this result in SAS VA?

 

Kind regards,

 

Tjalle

5 REPLIES 5
FredrikE
Rhodochrosite | Level 12

Hi!

one way is to create a new variable for each code and then (on the variables properties); set the aggregation to max.

 

Variable Max0:

IF coded_answer = 0 return 0 

ELSE .

 

Variable Max1:

IF coded_answer = 1 return 1 

ELSE .

 

Variable Max(n):

IF coded_answer = n return n 

ELSE .

 

//Fredrik

tjalleph
Calcite | Level 5

Hi FredrikE, 

 

Thanks for taking the time to respond. I haven't tried your solution yet, I will when I get back to SAS VA.

 

However, I do not see yet how this is giving me a distinct count of the IDs. Could you explain me how that would work in this code?

 

Kind regards,

 

Tjalle

 

FredrikE
Rhodochrosite | Level 12

Hi!

Maybe I was a bit too fast here, not understanding your logic fully 🙂

 

For the first table, why is the value for 0='1' and not '4'?

Resulting tables    
  max = 0  max = 1 max = 2
Total 1 2 1

 

If so one way is to create the new variable (0,1,2...n) by assigning the ID to them if the coded_answer is correct and then set the aggregation so distinc count.

 

Variable unique0:

IF coded_answer = 0 return ID 

ELSE .

 

Variable unique1:

IF coded_answer = 1 return ID 

ELSE .

 

Variable unique(n):

IF coded_answer = n return ID 

ELSE .

 

//Fredrik

tjalleph
Calcite | Level 5

Hi Fredrik,

 

I think we're getting closer to me asking the correct question 🙂

 

Answer to your question:

 

For the first table, why is the value for 0='1' and not '4'?

Resulting tables  
 max = 0 max = 1max = 2
Total121

 

The first value at max=0 is 1, because for only one ID the max(coded_answers)=0 over all selected rows. Therefore, the values in this table are a count, and do not refer to a specific ID. The other resulting tables are also counts of distinct ID. Cat A is in all 4 IDs and therefore there are 4 counts, but Cat E is only in one ID, and therefore there is only 1 count. 

 

 

I'm looking for the way to first aggregate to the max per ID, and then distinct count the IDs per categorized max (in this example, the values being 0, 1 or 2). It should do so dynamically, based on the grouping/slicing at hand.

 

Regards,

 

Tjalle

FredrikE
Rhodochrosite | Level 12

Hi!

Been of for a week...:)

 

If you are using SAS VA8.3 you can create an aggregated table with your max and then use this table as input for further calculations.

 

Don't know if this helps you, but i don't think you can do everything by just adding calculations and not the aggregated table.

 

Havn't tested any more, but maybe this can help you somehow 🙂

 

//Fredrik

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 3087 views
  • 0 likes
  • 2 in conversation