BookmarkSubscribeRSS Feed
art297
Opal | Level 21

: Here is yet one more try, this time independent of the number of variables and with a final reordering of the resulting variable names.  I did, however, change the spelling of "response".  The only thing that would have to be changed is the initial maximum number of response variables if it is ever greater than 999:

data have;

  infile cards truncover;

  input (Id Question response1-response999) ($);

  cards;

fgg45 A1 YES

fgg45 S1 YES

fgg45 S2 NO NO NO NO NO NO

fgg45 G5 YES YES YES YES YES YES YES

fgg45 G6 NO

gg453 S1 YES YES

gg453 S2 NO NO NO NO NO NO NO

gg453 G5 YES YES YES

gg453 G6 NO NO NO NO

;

proc sort data=have;

by id question;

run;

proc transpose data=have

  out=need (where=(not missing(col1)));

  by id question;

  var response:;

run;

data need (drop=question);

  set need;

  _name_=catt(Question,"_",substr(_name_,anydigit(_name_)));

run;

proc sql noprint;

  select _name_ into :reorder

    separated by " "

      from need

        order by _name_

  ;

quit;

proc transpose data=need out=want (drop=_:);

  by id;

  var col1;

run;

data want;

  retain id &reorder.;

  set want;

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
  • 15 replies
  • 2569 views
  • 0 likes
  • 6 in conversation