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;
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;
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.
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;
Many thanks guys!
NickR it works great! You saved me lots of time.
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.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.