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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1074 views
  • 0 likes
  • 3 in conversation