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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 15 replies
  • 5937 views
  • 0 likes
  • 6 in conversation