DATA Step, Macro, Functions and more

Return the variable name for some condition

Reply
Occasional Contributor
Posts: 8

Return the variable name for some condition

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.

Super Contributor
Posts: 275

Re: Return the variable name for some condition

data watn;

  set have;

  array var Q:;

  length yes $10;

  do over var;

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

  end;

  run;

Super User
Super User
Posts: 6,500

Re: Return the variable name for some condition

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

Ask a Question
Discussion stats
  • 2 replies
  • 156 views
  • 0 likes
  • 3 in conversation