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

Dear SAS expert

 

I would like to categorize "connected" 1's according to have_var (a 1 increment for every new connected 1s). I have tried to get it to work but no succes thus far. I have managed to create a count which starts over with each category (current_succes) but this does not suffice. Can anyone help me with the code for want_var given the following example data?

 

data have;
input have_var current_succes want_var;
datalines;
. . .
. . .
1 1 1
1 2 1
. . .
1 1 2
. . .
1 1 3
1 2 3
1 3 3
. . .
;
run;

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

If so then how about

 

data have;
input v;
datalines; 
2
1
3
1
1
1
4
5
6
1
1
;

data want(drop = ww);
   set have;
   by v notsorted;
   if v ne 1 then w = .;
   if v = 1 and first.v then do;
      w = ww + 1;
	  ww + 1;
   end;
   retain w;
run;

 

Result:

Obs v  w 
1   2  . 
2   1  1 
3   3  . 
4   1  2 
5   1  2 
6   1  2 
7   4  . 
8   5  . 
9   6  . 
10  1  3 
11  1  3 

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

How can we determine what you are doing when there are lines such as 

 

...

 

in your input data? Can you give us actual (or made-up) data in place of the ... ? Or can you explain more clearly what you are looking for?

--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

I don't quite understand this. 

 

Do you want want_var to increment when have_var = 1 and the previous have_var = 1 ? 

 

Otherwise, please explain further.

mgrasmussen
Quartz | Level 8

Every time there is a 1 or consecutive 1s I want to introduce a new number. The dataset is sorted. Actually what I really want is just to categorize 1s (only 1 or consecutive) in unique categories. It could in principle also be using letters. For example:

 

have_var number_categorization letter_categorization

. . .

1 1 A

. . .

1 2 B

1 2 B

1 2 B

. . .

. . .

. . .

1 3 C

1 3 C

 

The 2 variables to the right above displays the data that I would like to have - I don't have these two variables. The data that I have is only the variable on the left.

 

Missing values could be changed to 0's if this in terms of code would make a difference.

 

Am I still failing to make sense?

PeterClemmensen
Tourmaline | Level 20

We're getting there 😉

 

So, in between the 1's, do you want missing values or does that not matter?

mgrasmussen
Quartz | Level 8

I apologize for being unclear.

 

Yes, missing values between the 1's 🙂

 

The code needs to work for data exactly like the example I provided.

PeterClemmensen
Tourmaline | Level 20

Have you seen my code below?

PeterClemmensen
Tourmaline | Level 20

If so then how about

 

data have;
input v;
datalines; 
2
1
3
1
1
1
4
5
6
1
1
;

data want(drop = ww);
   set have;
   by v notsorted;
   if v ne 1 then w = .;
   if v = 1 and first.v then do;
      w = ww + 1;
	  ww + 1;
   end;
   retain w;
run;

 

Result:

Obs v  w 
1   2  . 
2   1  1 
3   3  . 
4   1  2 
5   1  2 
6   1  2 
7   4  . 
8   5  . 
9   6  . 
10  1  3 
11  1  3 
mgrasmussen
Quartz | Level 8

Yes, thanks. However, for some reason my SAS is not working right now (poor timing).

 

But would your code still work given the following data:

 

data have;

input v;

datalines;

.

1

.

1

1

1

.

.

.

1

1

;

run;

 

Notice that there has to be . (missings) between the 1s in the input data.

PaigeMiller
Diamond | Level 26

@mgrasmussen wrote:

Every time there is a 1 or consecutive 1s I want to introduce a new number. The dataset is sorted. Actually what I really want is just to categorize 1s (only 1 or consecutive) in unique categories. It could in principle also be using letters. For example:

 

have_var number_categorization letter_categorization

. . .

1 1 A

. . .

1 2 B

1 2 B

1 2 B

. . .

. . .

. . .

1 3 C

1 3 C

 

The 2 variables to the right above displays the data that I would like to have - I don't have these two variables. The data that I have is only the variable on the left.

 

Missing values could be changed to 0's if this in terms of code would make a difference.

 

Am I still failing to make sense?


You are failing to make sense to me. What are the rules? Please provide example data without the ... (triple dots), so we can see exactly what this would look like, data and desired output.

--
Paige Miller

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
  • 9 replies
  • 691 views
  • 0 likes
  • 3 in conversation