Help using Base SAS procedures

Sas mixes variables after transposing

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

Sas mixes variables after transposing

Hello All,

I am new to SAS and I am facing a problem.

I have a database with three columns, Code,Date,Volume. Ihave them sorted by code and date. When I transpose the data and setting the dates as variables they get mixed. So instead of having _31DEC2001, _31JAN2002, _28FEB2002 and so on I get: _30APR2008, _31OCT2002, _31MAR2003 etc.

Is there any way to arrange the variables(columns) so they will be in the correct date order?

I am attaching a sample file

I am transposing it as follows:

proc transpose data=yuri out=yuri2;

by code;

id date;

var volume;

run;


Accepted Solutions
Solution
‎07-08-2011 01:07 PM
Frequent Contributor
Posts: 81

Sas mixes variables after transposing

As data_null_ said, creating dummy BY group values would help.

proc sql;

          create table yuri_ as select distinct date,0.1 as code from yuri;

quit;

 

data yuri2;

          set yuri yuri_;

run;

 

proc sort; by code date; run;

 

proc transpose data=yuri2 out=yuri3(where=(code^=0.1));

          by code;

          id date;

          var volume;

run;

View solution in original post


All Replies
Respected Advisor
Posts: 3,780

Sas mixes variables after transposing

The variables, defined by the ID statement, are created in the same order new values of the ID variable are processed.  If the first CODE does not have all the dates then order of new vars may not be what you want/expect.  If the values of date are character they probably don't sort in data order, again not what you want.

I sometimes create a dummy BY group that has all the ID values in the order I prefer and use that to get the desired order.  You can search for other threads that address this issue and provide examples.

Solution
‎07-08-2011 01:07 PM
Frequent Contributor
Posts: 81

Sas mixes variables after transposing

As data_null_ said, creating dummy BY group values would help.

proc sql;

          create table yuri_ as select distinct date,0.1 as code from yuri;

quit;

 

data yuri2;

          set yuri yuri_;

run;

 

proc sort; by code date; run;

 

proc transpose data=yuri2 out=yuri3(where=(code^=0.1));

          by code;

          id date;

          var volume;

run;

Frequent Contributor
Posts: 75

Sas mixes variables after transposing

Many thanks guys!

NickR it works great! You saved me lots of time.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 129 views
  • 3 likes
  • 3 in conversation