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

I have data that is set up like this:

Pers Year Month Variable Value 
AAA 2001 01 Var1 100
AAA 2001 01 Var2 200
AAA 2001 06 Var1 110
AAA 2001 06 Var2 210
AAA 2002 01 Var1 120
AAA 2002 01 Var2 .
BBB 2001 01 Var1 100
BBB 2001 01 Var2 200
BBB 2001 06 Var1 110
BBB 2001 06 Var2 210
BBB 2002 01 Var2 220

I would like data that looks like this:

Pers Year Month Var1 Var2 
AAA 2001 01 100 200
AAA 2001 06 110 210
AAA 2002 01 120 .
BBB 2001 01 100 200
BBB 2001 06 110 210
BBB 2002 01 . 220

How can I do this in SAS?

Note that in the input data, above, Person BBB is missing an observation for 2002-01 Var1, but the output data has returned a missing value in the last line, i.e. ".".

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

Hi

You can use the TRANSPOSE procedure to do this. See example below:

data have;
  infile cards dlm=",";
 
input
    pers $ Year Month Variable $ Value
  ;
cards;
AAA,2001,01,Var1,100
AAA,2001,01,Var2,200
AAA,2001,06,Var1,110
AAA,2001,06,Var2,210
AAA,2002,01,Var1,120
AAA,2002,01,Var2,.
BBB,2001,01,Var1,100
BBB,2001,01,Var2,200
BBB,2001,06,Var1,110
BBB,2001,06,Var2,210
BBB,2002,01,Var2,220
;
proc sort data=have;
  by pers year month;
run;

proc transpose
 
data=have
 
out=want(drop=_name_)
;
  by pers year month;
  id Variable;
  var Value;
run;

View solution in original post

2 REPLIES 2
BrunoMueller
SAS Super FREQ

Hi

You can use the TRANSPOSE procedure to do this. See example below:

data have;
  infile cards dlm=",";
 
input
    pers $ Year Month Variable $ Value
  ;
cards;
AAA,2001,01,Var1,100
AAA,2001,01,Var2,200
AAA,2001,06,Var1,110
AAA,2001,06,Var2,210
AAA,2002,01,Var1,120
AAA,2002,01,Var2,.
BBB,2001,01,Var1,100
BBB,2001,01,Var2,200
BBB,2001,06,Var1,110
BBB,2001,06,Var2,210
BBB,2002,01,Var2,220
;
proc sort data=have;
  by pers year month;
run;

proc transpose
 
data=have
 
out=want(drop=_name_)
;
  by pers year month;
  id Variable;
  var Value;
run;
Jagadishkatam
Amethyst | Level 16

Hi Andrew,

Alternatively the same output could be obtained by arrays.

data have;

input Pers$ Year Month Variable$ Value ;

ord=input(compress(variable,,'kd'),1.);

cards;

AAA 2001 01 Var1 100

AAA 2001 01 Var2 200

AAA 2001 06 Var1 110

AAA 2001 06 Var2 210

AAA 2002 01 Var1 120

AAA 2002 01 Var2 .

BBB 2001 01 Var1 100

BBB 2001 01 Var2 200

BBB 2001 06 Var1 110

BBB 2001 06 Var2 210

BBB 2002 01 Var2 220

;

proc sort data=have;

by pers year month variable;

run;

data want;

set have;

retain var1 var2;

  by pers year month variable;

  array vs(2) var1 var2;

  if first.month then do;

  do i = 1 to 2;

  vs(i)=.;

  end;

  end;

  vs(ord)=value;

  if last.month;

run;

Hope it helps.

Thanks,

Jag

Thanks,
Jag

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1171 views
  • 3 likes
  • 3 in conversation