BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
LarissaW
Obsidian | Level 7

My data looks like this:

data dat;
   input id type type2 type3 type4 type5 type6;
   datalines;
   1 1 0 0 0 0 0
   2 3 1 0 0 0 0 
   3 4 5 0 0 0 0
   4 2 0 0 0 0 0
   5 6 3 0 0 0 0 
   6 7 2 1 6 0 0
   ;
run;

I want to create new variables based on the new classification of the type, like: 

group A: have either 1 or 2 or 3 in variables type-type6; 

group B: have either 5 or 6 in variables type-type6;

group C: have 4 in variables type-type6;

group 😧 have 7 in variables type-type6;

Each group is a single new column.

Is there a fast way to get the result I wanted?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

First hint is do not indent lines of data.  At best it just waste screen space and makes your program harder to read.  At worse the spaces could get into the values or through off the input statement and cause it to read the wrong locations on the line.

 

To remind yourself (and prevent the SAS editor from auto indenting when you start a new line) do not indent the DATALINES statement either.

 

Second hint is use variable lists where they make sense to make your coding easier.

data have;
   input id type1-type6 ;
datalines;
1 1 0 0 0 0 0
2 3 1 0 0 0 0 
3 4 5 0 0 0 0
4 2 0 0 0 0 0
5 6 3 0 0 0 0 
6 7 2 1 6 0 0
;

Final hints:

Use  the WHICHN() function to test if a value exists.

Use Boolean expressions to create 0/1 values.

data want;
  set have;
  groupa = whichn(1, of type1-type6) or whichn(2, of type1-type6) or whichn(3, of type1-type6);
  groupb = whichn(5, of type1-type6) or whichn(6, of type1-type6);
  groupc = 0^=whichn(4, of type1-type6);
  groupd = 0^=whichn(7, of type1-type6);
run;

Results

Tom_0-1682394919408.png

 

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

First hint is do not indent lines of data.  At best it just waste screen space and makes your program harder to read.  At worse the spaces could get into the values or through off the input statement and cause it to read the wrong locations on the line.

 

To remind yourself (and prevent the SAS editor from auto indenting when you start a new line) do not indent the DATALINES statement either.

 

Second hint is use variable lists where they make sense to make your coding easier.

data have;
   input id type1-type6 ;
datalines;
1 1 0 0 0 0 0
2 3 1 0 0 0 0 
3 4 5 0 0 0 0
4 2 0 0 0 0 0
5 6 3 0 0 0 0 
6 7 2 1 6 0 0
;

Final hints:

Use  the WHICHN() function to test if a value exists.

Use Boolean expressions to create 0/1 values.

data want;
  set have;
  groupa = whichn(1, of type1-type6) or whichn(2, of type1-type6) or whichn(3, of type1-type6);
  groupb = whichn(5, of type1-type6) or whichn(6, of type1-type6);
  groupc = 0^=whichn(4, of type1-type6);
  groupd = 0^=whichn(7, of type1-type6);
run;

Results

Tom_0-1682394919408.png

 

LarissaW
Obsidian | Level 7

Thank you for the solution! I'd like to know if I didn't change the title "type" to "type1", can I use "of type:" to indicate all the type-type6?

Tom
Super User Tom
Super User

You could use type: as long as there are not any other variables that start with type.

Or you could use type type2-type6.

 

But why would you want to break the naming pattern of the TYPE variables by not including the numeric suffix on the first variable?

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
  • 3 replies
  • 768 views
  • 1 like
  • 2 in conversation