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

Hello,

I will try it as simply as possible at first...

I have this dataset:

group   n_animals  age_difference

1          8                 1                        

2          45               2                     

4          19               1

 

And I would like to programme this: if group=1 and n_animals<30 then create variable g2 as group+1 “but only when the next group (group=1+1) has age difference < 2”.

 

The part in quotes is something I cannot figure out. I would really appreciate if the syntax stayed this way so I do not have to start from scratch.

1 ACCEPTED SOLUTION

Accepted Solutions
WarrenKuhfeld
Ammonite | Level 13

Look up the POINT= option on the SET statement. You can have one SET statement (with the POINT= option) that reads ahead and one ordinary SET statement that reads the current row to process. There are other ways too. Be sure to not use POINT= to try to read beyond the data.

View solution in original post

5 REPLIES 5
WarrenKuhfeld
Ammonite | Level 13

Look up the POINT= option on the SET statement. You can have one SET statement (with the POINT= option) that reads ahead and one ordinary SET statement that reads the current row to process. There are other ways too. Be sure to not use POINT= to try to read beyond the data.

opiczak
Fluorite | Level 6
Thank you, Warren. I quickly went through some guide and I am pretty sure this can solve my problem. Have a nice day.
Ksharp
Super User

Assuming I understand your question.

 

data have;
input group   n_animals  age_difference;
cards;
1          8                 1                        
2          45               2                     
4          19               1
;


data want;
merge have have(keep=age_difference rename=(age_difference=_age_difference) firstobs=2);
if group=1 and n_animals<30 and  _age_difference< 2 then g2=group+1;
drop _age_difference;
run;
opiczak
Fluorite | Level 6
Thank you very much! I think this can work. I would like to mark your reply as a solution, but apparently there can be only one (?). So I hope you do not mind.
Ksharp
Super User
I do not mind and do not care . 🙂

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 1022 views
  • 2 likes
  • 3 in conversation