BookmarkSubscribeRSS Feed
jenny_li
Calcite | Level 5

Hi,

I am a new learner of SAS, this maybe a easy stuff for you.

my dataset look like this

ID      Q1    Q2   Q3   Q4  Q5

1       NA    Y     N     N    Y

2       Y      Y     N     NA  NA

3       N      N     Y     Y     N

and I want to make the dataset into the following way

ID      Q1    Q2   Q3   Q4  Q5       Yes_question

1       NA    Y     N     N    Y         Q2 Q5

2       Y      Y     N     NA  NA       Q1 Q2

3       N      N     Y     Y     N        Q3 Q4

I have tried the following code

data dataset2;

set dataset;

yes_question2=" ";

retain yes_question2;

array yes(5) Q1--Q5;

do i = 1 to 5;

if yes(i)="Y" then yes_question=cat(yes_question2," ",vname(yes(i)));

yes_question2=yes_question;

end;

run;

But yes_question2 keeps equal to blanks and I can only get the result of blank+last Yes question.

Any simple code to make this possible?

Thanks for the help.

2 REPLIES 2
slchen
Lapis Lazuli | Level 10

data watn;

  set have;

  array var Q:;

  length yes $10;

  do over var;

    if var='Y' then Yes=catx(' ',yes,vname(var));

  end;

  run;

Tom
Super User Tom
Super User

CAT() function does not trim so you are essentially reassigning the empty value in YES_QUESTION to itself.

yes_question=catX(' ',yes_question,vname(yes(i)));

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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