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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 1771 views
  • 1 like
  • 2 in conversation