Help using Base SAS procedures

Auto rename rows when (or before) running proc transpose

Reply
Occasional Contributor
Posts: 6

Auto rename rows when (or before) running proc transpose

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     

PROC Star
Posts: 7,492

Re: Auto rename rows when (or before) running proc transpose

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.

Trusted Advisor
Posts: 1,137

Re: Auto rename rows when (or before) running proc transpose

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
Ask a Question
Discussion stats
  • 2 replies
  • 160 views
  • 0 likes
  • 3 in conversation