BookmarkSubscribeRSS Feed
NumberMuncher
Calcite | Level 5

Hello all,

I've seen similar questions but none seem to be what I am looking for.

I have one dataset with 2 variables I'd like to compare by ID. I can use proc compare but the rows start at different dynamic locations. I can't figure out how to either move my compare variable (x2) up to the correct row location or how to lag it behind to start at a dynamic location. Each id has a different number of groups.

This is the data I have:

idgroupx1x2
114/16/2021 
124/19/2021 
134/28/2021 
146/4/2021 
11 4/16/2020
12 4/19/2021
13 4/28/2021
14 6/4/2021
218/12/2020 
229/16/2020 
2310/8/2020 
242/8/2021 
259/29/2021 
21 8/12/2020
22 9/16/2021
23 10/8/2020
24 2/8/2021
25 9/29/2021

And this is what I want (or at least a way to start the proc compare to lag by the max range in each id's group number):

idgroupx1x2x3x4x5
114/16/2021 4/16/20214/16/20200
124/19/2021 4/19/20214/19/20211
134/28/2021 4/28/20214/28/20211
146/4/2021 6/4/20216/4/20211
11 4/16/2020   
12 4/19/2021   
13 4/28/2021   
14 6/4/2021   
218/12/2020 8/12/20208/12/20201
229/16/2020 9/16/20209/16/20210
2310/8/2020 10/8/202010/8/20201
242/8/2021 2/8/20212/8/20211
259/29/2021 9/29/20219/29/20211
21 8/12/2020   
22 9/16/2021   
23 10/8/2020   
24 2/8/2021   
25 9/29/2021   

 

Any help in the right direction would be awesome. Please and thank you!

2 REPLIES 2
PaigeMiller
Diamond | Level 26

UNTESTED CODE

 

data want;
    merge have(keep=id group x1 where=(not missing(x1))) 
        have(keep=id group x2 where=(not missing(x2)));
    by id group;
run;
    

This code eliminates the redundant columns in your requested data set. This code makes no attempt to compute x5 as you did not provide an explanation of what it is.

 

If you want tested code, please provide data as SAS data step code, which you can type in yourself, or use these instructions. Do not provide data in any other format.

--
Paige Miller
NumberMuncher
Calcite | Level 5

Thanks! This was very helpful.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2 replies
  • 738 views
  • 0 likes
  • 2 in conversation