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)));

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