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

Hello,

 

I want to rearrange the year column as you see below, in the order as the lastest date as first, for instance, Q3_2016, Q2_2016, Q1_2016, Q4_2015, Q3_2015, Q2_2015. One thing is that, the quarter keeps rolling, that means, when we are at the beginning of 2017, I will have Q4_2016 rolled into the dataset.  then how to make it? Thanks a lot!

 

data old;
input year $ resp ;
datalines;
Q1_2015 0.35
Q2_2015 0.55
Q3_2015 0.65
Q4_2015 0.75
Q1_2016 0.47
Q2_2016 0.58
Q3_2016 0.69
;run;

proc transpose data=old out=new;
id year;
var resp ;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

You need to use a variable which sorts the way you want it in your ID statement.

 

Your YEAR variable is character and therefore all Q1... values will be before Q2...

 

Below an approach which should give you what you want.

data old;
  input year $ resp;
  datalines;
Q1_2015 0.35
Q2_2015 0.55
Q3_2015 0.65
Q4_2015 0.75
Q1_2016 0.47
Q2_2016 0.58
Q3_2016 0.69
;
run;

data need(drop=fmtname start label) cntlin(keep=fmtname start label);
  set old;
  format dt yyq6.;
  dt=input(substr(year,4,4)||substr(year,1,2),yyq6.);
  
  retain fmtname 'myQuarters';
  start=dt;
  label=year;
run;

proc format cntlin=cntlin;
run;

proc transpose data=need out=new;
  id dt;
  format dt myQuarters.;
  var resp;
run;

View solution in original post

3 REPLIES 3
Reeza
Super User

You talk about columns but show rows of data. 

How exactly do you want your output to be?

zhangda
Fluorite | Level 6

I just added a new piece of code to clarify my problem, sorry for confusing you.

Patrick
Opal | Level 21

You need to use a variable which sorts the way you want it in your ID statement.

 

Your YEAR variable is character and therefore all Q1... values will be before Q2...

 

Below an approach which should give you what you want.

data old;
  input year $ resp;
  datalines;
Q1_2015 0.35
Q2_2015 0.55
Q3_2015 0.65
Q4_2015 0.75
Q1_2016 0.47
Q2_2016 0.58
Q3_2016 0.69
;
run;

data need(drop=fmtname start label) cntlin(keep=fmtname start label);
  set old;
  format dt yyq6.;
  dt=input(substr(year,4,4)||substr(year,1,2),yyq6.);
  
  retain fmtname 'myQuarters';
  start=dt;
  label=year;
run;

proc format cntlin=cntlin;
run;

proc transpose data=need out=new;
  id dt;
  format dt myQuarters.;
  var resp;
run;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 798 views
  • 0 likes
  • 3 in conversation