BookmarkSubscribeRSS Feed
Leahk
Calcite | Level 5

I have a data set that looks something like this:

idnum   var1   var2   var3   var4   var5   var6   var7   var8   var9   var10

1           a         b       12      e        2        3         4       5        6         7

2           v        f         15      g       5         6        7        8        9         10

3 ...

 

I want to combine var6-10 under one headin, but still have them seperate (if that makes sense). So it would end up something like this:

 

idnum   var1   var2   var3   var4   var5  newvar 

1           a        b        12      e        2      var6    3

                                                             var7    4

                                                             var8    5

                                                             var9    6

                                                             var10  7

2           v        f        15      g        5       var6    6

                                                             var7    7

                                                             var8    8

                                                             var9    9

                                                             var10  10

 

Is this even possible? I've attempted PROC TRANSPOSE but I can't figure it out.

Any help would be much appreciated!

4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

Please try arrays

 

data have;
input idnum   var1$   var2$   var3   var4 $  var5   var6   var7   var8   var9   var10;
cards;
1           a         b       12      e        2        3         4       5        6         7
2           v        f         15      g       5         6        7        8        9         10
;

data want;
set have;
by idnum;
array vs(5) var6   var7   var8   var9   var10;
do i = 1 to 5;
if i ne 1 then call missing(idnum, var1,var2,var3,var4,var5);
newvar=cat(strip(vname(vs(i))),' ',vs(i));
output;
end;
drop var6-var10 i;
run;
Thanks,
Jag
Reeza
Super User

Is this for a report or display purposes, or do you need a data set in this format?

PGStats
Opal | Level 21

Something like this perhaps:

 

proc transpose data=have out=want name=namevar prefix=v;
by idnum   var1   var2   var3   var4   var5 notsorted;
var var6 -- var10;
run;

proc print data=want;
by idnum   var1   var2   var3   var4   var5 notsorted;
id idnum   var1   var2   var3   var4   var5;
run;

/* Or perhaps */
data want2;
set want;
newvar = cat(put(namevar,$6.),put(v1,3.));
drop namevar v1;
run;

proc print data=want2;
by idnum   var1   var2   var3   var4   var5 notsorted;
id idnum   var1   var2   var3   var4   var5;
run;
PG
Cynthia_sas
SAS Super FREQ

Hi:

  If all you want is a report, here is another approach that shows 2 PROC REPORT steps (one with summarized total, one without) based on restructured data.

 

data start;

infile datalines dlm=',' dsd;

input idnum var1 $ var2 $ var3 var4 $ var5 var6 var7 var8 var9 var10;

return;

datalines;

1,a,b,12,e,2,3,4,5,6,7

2,v,f,15,g,5,6,7,8,9,10

3,q,w,11,h,1,1,1,1,1,1

;

run;

 

data final(keep=idnum var1 var2 var3 var4 var5 varnum varval);

set start;

** restructure data by making 1 obs for every var from 6-10;

** could put this in DO loop, but keeping it simple for this program;

varnum = 'var6';

varval = var6;

output;

 

varnum = 'var7';

varval = var7;

output;

 

varnum = 'var8';

varval = var8;

output;

 

varnum = 'var9';

varval = var9;

output;

 

varnum = 'var10';

varval = var10;

output;

run;

 

proc print data=final;

title 'See final structure';

run;

 

proc report data=final nowd;

title '1) Just Display the Other variables';

column idnum var1 var2 var3 var4 var5 ('Other Vars' varnum varval);

define idnum / order;

define var1 / order;

define var2 / order;

define var3 / order;

define var4 / order;

define var5 / order;

define varnum / display 'Var Name';

define varval / display 'Var Value';

run;

 

proc report data=final nowd;

title '2) Or, Add them Up';

column idnum var1 var2 var3 var4 var5 ('Other Vars' varnum varval);

define idnum / order;

define var1 / order;

define var2 / order;

define var3 / order;

define var4 / order;

define var5 / order;

define varnum / display 'Var Name';

define varval / sum 'Var Value';

break after idnum / summarize;

run;

 

 If all you want is a report, #1 is that report, but needs a separate row in the data for every var, var6-var10, where the values of var1-var5 stay the same on those rows (see the PROC PRINT output for the restructured data).

 

  Then report #2 summarizes var6-var10 for every IDNUM.

 

cynthia

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
  • 4 replies
  • 1015 views
  • 0 likes
  • 5 in conversation