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

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
Fluorite | Level 6

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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 253 views
  • 1 like
  • 2 in conversation