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

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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