BookmarkSubscribeRSS Feed
Brandon16
Obsidian | Level 7

Hi,

 

I have this dummy code (only a section as this goes up to December) below where I have sum for each variable. How do I group this into one variable so my next part of code referencing the table will look at variable 'x' instead of having to type out each individual variable name again?

 

I basically want 

sum(jancount) as onecount,
sum(janvalue) as onevalue,
sum(febcount) as twocount,
sum(febvalue) as twovalue,
sum(marcount) as threecount,
sum(marvalue) as threevalue as all_data

 

I hope that makes sense.

 

proc sql;
create table test.test_cas as
select
pydate,
sum(jancount) as onecount,
sum(janvalue) as onevalue,
sum(febcount) as twocount,
sum(febvalue) as twovalue,
sum(marcount) as threecount,
sum(marvalue) as threevalue
from test.testing
group by pydate;

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Your making it difficult for yourself.  It is never a good idea to put data observations in colums - i.e. rather than this:

jancount  janvalue  febcount febvalue

 

Which is very hard to program with, and not good data storage (what happens at end of year, overlaps etc. 

A simpler, better solution:

Date   Count  Value

 

With this you can simply summarise the data using proc means/summary.  The date value can be formatted as month, but would be expandable across years, sub dividable out and generally far more usable.  Just your code you present there, having to type in manually each variable name is a real pain, SQL is designed to work with normalised data (as I showed), not transposed, and even SAS which has some shortcuts doesn't work that great with transposed data.

Brandon16
Obsidian | Level 7

The data is already set out that way in production (I just sent dummy data across) and I want to create a tabulate on the back of the data but there will be too many dimensions (unless I am doing the tabulate wrong).  

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