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

I want to transpose the following table without using proc transpose:

 

Original Table:

ID facility

1  Swimming Pool

1  SPA

2  Gym room

2  restaurant

2  bar

3  Room

 

Expected Table:

ID    facility_1               facility_2       facility_3

1      Swimming Pool    SPA              

2      Gym room            restaurant     bar

3      Room

 

The number of facilities for each ID is unknown.

 

Can someone please help me out?

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12
data have;
    length facility $20.;
    infile cards dlm=',' dsd;
    input ID facility;
    cards;
1,Swimming Pool
1,SPA
2,Gym room
2,restaurant
2,bar
3,Room
    ;
run;

proc sql noprint;
    SELECT max(nfac) INTO :nfac TRIMMED
    FROM (
    SELECT count(facility) AS nfac
    FROM have
    GROUP BY ID
    )
    ;
quit;

data want;
    set have;
    by ID;
    retain facility1-facility&nfac.;

    array fac(&nfac.) $20. facility1-facility&nfac.;

    if first.ID then do;
        call missing(of fac(*));
        i=1;
    end;
    else i+1;

    fac(i)=facility;

    if last.ID;

    drop i facility;
run;

View solution in original post

5 REPLIES 5
gamotte
Rhodochrosite | Level 12
What's wrong with proc transpose ?
Jonathanzz
Obsidian | Level 7

I am going to transpose the dataset inside SAS enterprise miner using code editor, Howeve, it seems SAS enterprise miner does not support proc transpose.

gamotte
Rhodochrosite | Level 12
data have;
    length facility $20.;
    infile cards dlm=',' dsd;
    input ID facility;
    cards;
1,Swimming Pool
1,SPA
2,Gym room
2,restaurant
2,bar
3,Room
    ;
run;

proc sql noprint;
    SELECT max(nfac) INTO :nfac TRIMMED
    FROM (
    SELECT count(facility) AS nfac
    FROM have
    GROUP BY ID
    )
    ;
quit;

data want;
    set have;
    by ID;
    retain facility1-facility&nfac.;

    array fac(&nfac.) $20. facility1-facility&nfac.;

    if first.ID then do;
        call missing(of fac(*));
        i=1;
    end;
    else i+1;

    fac(i)=facility;

    if last.ID;

    drop i facility;
run;
Jonathanzz
Obsidian | Level 7
Thanks Gamotte!
I have one more request. Can it be done without using PROC statement and only using data step since SAS Enterprise Miner do not support any PROC statement.
Thank you so much!!!
gamotte
Rhodochrosite | Level 12

Try replacing the proc sql with the following data step :

 

data _NULL_;
    set have(keep=ID);
    by ID;
    retain nfac;

    if _N_=1 then nfac=0;

    if first.ID then i=1;
    else i+1;

    if last.ID and i>nfac then do;
        nfac=i;
        call symputx("nfac", nfac);
    end;
run;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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