BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
gpv2000
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

2 REPLIES 2
ballardw
Super User

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;

gpv2000
Calcite | Level 5

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1928 views
  • 1 like
  • 2 in conversation