BookmarkSubscribeRSS Feed
viuf
Calcite | Level 5

Hi all

I have a dataset as shown below. I would like to do a transpose so the variables will come from the column "name" and the values from "val1".

nameval1val2val3val4
Income819772819772819772819772
Interestrate383884383884383884383884
BANK422388422388422388422388
Børnefamilieydelse13500135001350013500
Pensionsindtægter0000
Housing317914303766300673297636
Interestrate75800758007580075800
Husleje52800528005280052800
Interestrate4800480048004800
El5600560056005600
Kabel TV10800108001080010800

I'm planning to do a proc tranpose like this and it seems to do the job fine.

proc transpose data=want

out=transpose

name=val1;

id name;

run;

My problem is that duplicates will occur and there is no way of knowing which. I therefore need a smart way to automatically rename these duplicates while (or before) running the Proc Transpose.

What I would like is either a solution where the new name is constructed on the basis of a foremer name (preferable) or a solution where the duplicates are numbered. I imagine it can look like this:

Result (based on group values):

Income     Income-Interestrate      ...     Housing     Housing-Interestrate

xxx                    xxx                              xxx                    xxxx

or maybe (based on number):

Income      Interestrate1      ...     Housing          Interestrate2

xxx                   xxx                         xxx               xxx

Hope someone can help - Thanks Smiley Happy     

2 REPLIES 2
art297
Opal | Level 21

If you look at your original dataset there is an id variable.  You could just capture it, when you create your dataset, and use it as a by variable when running proc transpose.

Jagadishkatam
Amethyst | Level 16

Hi,

in this case, you can count the number of repetitive name and assign the serial number adjacent to the name and then transpose it. I have followed the same procedure.

i hope this helps you.

nameval1val2val3val4
Income819772819772819772819772
Interestrate383884383884383884383884
BANK422388422388422388422388
Børnefamilieydelse13500135001350013500
Pensionsindtægter0000
Housing317914303766300673297636
Interestrate75800758007580075800
Husleje52800528005280052800
Interestrate4800480048004800
El5600560056005600
Kabel TV10800108001080010800

dataset have is above;

proc sort data=have;

    by name;

run;

data have_;

    set have;

    retain count;

    by name;

    if first.name then count=1;

    else count+1;

    if count > 1 then new_name=cats(name,count);

    else new_name=name;

run;

proc transpose data=have_ out=want name=var1;

    id new_name;

    var val1;

run;

proc print;

run;

Good Luck.

Thanks,

Jagadish

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!

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.

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
  • 687 views
  • 0 likes
  • 3 in conversation