DATA Step, Macro, Functions and more

Create a New Variable

Reply
Contributor
Posts: 42

Create a New Variable

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;

Super User
Super User
Posts: 9,416

Re: Create a New Variable

Posted in reply to Brandon16

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.

Contributor
Posts: 42

Re: Create a New Variable

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).  

Ask a Question
Discussion stats
  • 2 replies
  • 80 views
  • 0 likes
  • 2 in conversation