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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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