Solved
Highlighted
New Contributor
Posts: 2

Array with Do Until?

I need to create 3 variables(var1 var2 var3) based upon the occurrence of conditions within the subject. I have the input dataset example and output dataset example below. I think and array with do until might work but I am not sure how to implement. Any suggestions?

Input Dataset:

subject    None      Discomfort       Bad_Vision      Other
12345       1
12345                        1
12345                        1                         1
12345                        1                                             1
12345                        1                         1                  1
12345                                                                       1

Output Dataset:

Subject              Var1                 Var2               Var3
12345              NONE
12345              Discomfort
12345              Discomfort     Bad_vision
12345              Discomfort     Other
12345              Discomfort     Bad vision         Other
12345              Other

Accepted Solutions
Solution
a week ago
Super User
Posts: 13,358

Re: Array with Do Until?

Best is to provide example data in the form of a data step. Then there are no questions about variable type, name or characteristics. Also code can be tested against the supplied data.

Also best is to post code in a code box opened using the forum {I} icon to preserve formatting.

```data have;
input subject    None      Discomfort       Bad_Vision      Other;
datalines;
12345 1  .   .  .
12345 .  1   .  .
12345 .  1   1  .
12345 .  1   .  1
12345 .  1   1  1
12345 .  .   .  1
run;

data want;
set have;
array a None Discomfort Bad_vision Other;
array var{3} \$ 10;
count=0;
do i=1 to dim(a);
if a[i] = 1 then do;
count=count+1;
var[count]=vname(a[i]);
end;
end;
keep subject var: ;
run;
```

The above works for your example. If you ever have cases where NONE is one and the other variables are populated as well then the solution may require having a fourth variable.

the function VNAME gets the name of a variable referenced as an array element.

The VAR:  with the colon is a short cut list that refers to all variables whose names start with VAR;

All Replies
Solution
a week ago
Super User
Posts: 13,358

Re: Array with Do Until?

Best is to provide example data in the form of a data step. Then there are no questions about variable type, name or characteristics. Also code can be tested against the supplied data.

Also best is to post code in a code box opened using the forum {I} icon to preserve formatting.

```data have;
input subject    None      Discomfort       Bad_Vision      Other;
datalines;
12345 1  .   .  .
12345 .  1   .  .
12345 .  1   1  .
12345 .  1   .  1
12345 .  1   1  1
12345 .  .   .  1
run;

data want;
set have;
array a None Discomfort Bad_vision Other;
array var{3} \$ 10;
count=0;
do i=1 to dim(a);
if a[i] = 1 then do;
count=count+1;
var[count]=vname(a[i]);
end;
end;
keep subject var: ;
run;
```

The above works for your example. If you ever have cases where NONE is one and the other variables are populated as well then the solution may require having a fourth variable.

the function VNAME gets the name of a variable referenced as an array element.

The VAR:  with the colon is a short cut list that refers to all variables whose names start with VAR;

New Contributor
Posts: 2

Re: Array with Do Until?

Thank you. I will keep in mind the points you mentioned about data. Thanks again.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 2 replies
• 76 views
• 0 likes
• 2 in conversation