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

Hi,

 

I have a very large dataset (ID's with a row status each month) that I need to translate into an ID level dataset that provides when the status changed, starting from 0.

 

For example:

 

Large dataset:

 

IDstatusMonthCountry
1LFeb-06Eng
1LMar-06Eng
1LApr-06Eng
1LMay-06Eng
1EJun-06Eng
1NJul-06Eng
2LFeb-06Sco
2LMar-06Sco
2LApr-06Sco
2LMay-06Sco
2EJun-06Sco
3LFeb-06Wal
3LMar-06Wal
3LApr-06Wal
4LMay-06Wal
4EJun-06Wal
5LFeb-06Eng
5EMar-06Eng
5EApr-06Eng
5NMay-06Eng

 

What I need to get to:

 

IDMonthCountryStatus_EStatus_N
1Feb-06Eng45
2Feb-06Sco4.
3Feb-06Wal3.
4Feb-06Wal1.
5Feb-06Eng13

 

So for ID 1, a status of E was observed 4 rows down from the starting value (always L) and a status of N was observed 5 values down.

 

For ID 4, a status of E was observed 1 row down from starting value (always L) and a status of N was not observed in the history so is blank.

 

I also need to retain the first month as well as the country (static).

 

What is the most efficient way for me to get to this single ID level dataset with the additional variables?

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

This should be fairly efficient. Though, I don't understand why Status_E=3 in obs=3?

 

data have;
input ID status $ Month:monyy6. Country $;
format Month monyy6.;
datalines;
1 L Feb-06 Eng
1 L Mar-06 Eng
1 L Apr-06 Eng
1 L May-06 Eng
1 E Jun-06 Eng
1 N Jul-06 Eng
2 L Feb-06 Sco
2 L Mar-06 Sco
2 L Apr-06 Sco
2 L May-06 Sco
2 E Jun-06 Sco
3 L Feb-06 Wal
3 L Mar-06 Wal
3 L Apr-06 Wal
4 L May-06 Wal
4 E Jun-06 Wal
5 L Feb-06 Eng
5 E Mar-06 Eng
5 E Apr-06 Eng
5 N May-06 Eng
;

data want(drop=_: status);
    do _N_=0 by 1 until (last.id);
        set have;
        by id;
        if first.id then do;
            _Month   = Month;
            _Country = Country;
        end;
        if status='E' & Status_E=. then Status_E=_N_;
        if status='N' & Status_N=. then Status_N=_N_;
    end;

    Month   = _Month;
    Country = _Country;
run;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

This should be fairly efficient. Though, I don't understand why Status_E=3 in obs=3?

 

data have;
input ID status $ Month:monyy6. Country $;
format Month monyy6.;
datalines;
1 L Feb-06 Eng
1 L Mar-06 Eng
1 L Apr-06 Eng
1 L May-06 Eng
1 E Jun-06 Eng
1 N Jul-06 Eng
2 L Feb-06 Sco
2 L Mar-06 Sco
2 L Apr-06 Sco
2 L May-06 Sco
2 E Jun-06 Sco
3 L Feb-06 Wal
3 L Mar-06 Wal
3 L Apr-06 Wal
4 L May-06 Wal
4 E Jun-06 Wal
5 L Feb-06 Eng
5 E Mar-06 Eng
5 E Apr-06 Eng
5 N May-06 Eng
;

data want(drop=_: status);
    do _N_=0 by 1 until (last.id);
        set have;
        by id;
        if first.id then do;
            _Month   = Month;
            _Country = Country;
        end;
        if status='E' & Status_E=. then Status_E=_N_;
        if status='N' & Status_N=. then Status_N=_N_;
    end;

    Month   = _Month;
    Country = _Country;
run;
yabwon
Onyx | Level 15

Beautiful example of DoW-loop use!

All the best

Bart 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



PetePatel
Quartz | Level 8

This is a great example, thanks!

PeterClemmensen
Tourmaline | Level 20

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
  • 4 replies
  • 434 views
  • 3 likes
  • 3 in conversation