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