BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Richard_Gordon
Fluorite | Level 6

Hi,

I am looking for a proc sql approach to summarize a set of variables that have only 3 states of Valid, Missing, or OOR (out of range) values and represented by 0, 1, or 2, respectively.

 

In the data set below, there are two groups with four variables that have 3 states (0,1, or 2). 

 

data have;
length grp $1 v1 v2 v3 v4 4;
input grp v1 v2 v3 v4 ;
datalines;
1 0 0 0 1
1 0 0 0 1
1 0 1 0 1
1 0 1 0 1
2 1 1 0 2
2 1 1 0 2
2 1 2 2 2
2 1 2 2 2
;
run;

 

What proc sql command (or other, if needed) can generate the following output, which summarizes the count of each state by group by variable.

 

data want;
length grp $1 var $4 type0 type1 type2 4;
input grp var type0 type1 type2;
datalines;
1 v1 4 0 0
1 v2 2 2 0
1 v3 4 0 0
1 v4 0 4 0
2 v1 0 4 0
2 v2 0 2 2
2 v3 2 0 2
2 v4 0 0 4
;
run;

 

Thanking you in advance,

rg

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Here's an example that works for two variables, at least for your example data.

You would have to union each variable

 

proc sql;
   create table want as

   select grp, 'V1' as var,sum(v1=0) as type0,sum(v1=1) as type1,sum(v1=2) as type2
   from have
   group by grp
   union
   select grp, 'V2' as var,sum(v2=0) as type0,sum(v2=1) as type1,sum(v2=2) as type2
   from have
   group by grp

   ;
quit;

I really wouldn't relish doing this type of operation for many variables or levels of variables.

 

I am not going to make any claim to efficiency.

View solution in original post

6 REPLIES 6
Reeza
Super User

Can you use PROC FREQ? You can do it in SQL but it's definitely very very ugly.

 

 

 

 

Richard_Gordon
Fluorite | Level 6

Reeza,

So in SAS, we have the luxury of alternative approaches with procs designed for certain tasks.  Part of the reason for this post is to help me move some POC development work into non-SAS environments.

 

But really, this problem is stumping me from a sql perspective ... and ugly would make me not feel so bad for not seeing the solution 🙂

rg

 

 

Reeza
Super User

Part of the reason for this post is to help me move some POC development work into non-SAS environments.

 

I would probably visit the forum of those environments then instead of SAS. Replicating code, line by line/task by task is not efficient. Rather, determine processes and implement in new tools from scratch so that's its more efficient. You make less mistakes, it's overall faster and the results are better with this type of approach. 

 

The SQL approach we provide here may not be appropriate for your new environment. The new env may have windowing or partitioning functions that make this easier than using BASE SQL. 

 

 

 

 

Richard_Gordon
Fluorite | Level 6

I will keep that in mind, thank you.

ballardw
Super User

Here's an example that works for two variables, at least for your example data.

You would have to union each variable

 

proc sql;
   create table want as

   select grp, 'V1' as var,sum(v1=0) as type0,sum(v1=1) as type1,sum(v1=2) as type2
   from have
   group by grp
   union
   select grp, 'V2' as var,sum(v2=0) as type0,sum(v2=1) as type1,sum(v2=2) as type2
   from have
   group by grp

   ;
quit;

I really wouldn't relish doing this type of operation for many variables or levels of variables.

 

I am not going to make any claim to efficiency.

Richard_Gordon
Fluorite | Level 6

Ballardw

 

This works!

 

Fortunately, the grouping variable is the lowest dimension, so another join to a dim table will allow other aggregations for reporting. To your other comment, the structure of the command is conducive to automated code generation for an arbitrary array of variables, which could be executed in a variety of data frames.

 

Thank you for taking time to look at this ... you moved me along on my project!

rg

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 3848 views
  • 0 likes
  • 3 in conversation