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

Hi All,

 

I need help in programming part. I have dataset :

 

Dataset:

Obs  Subject    ADY   Avisitn    Avalc  

1       101           86      12            PR

2        101           186     17          PD

3        101           257     24          PR 

4        101           327     29          PR

5        101           382     32          PR

6        102           76       14          PR

7        102          128     17          CR

8        102          188     24          CR 

9        102           253    29          CR

 

I need a output these way:

 

Obs  Subject    ADY   Avisitn    Avalc  

1       101           86      12            PR

2        101           186     17          PD

3        101           257     24          PR 

5        101           382     32          PR

6        102           76       14          PR

7        102          128     17          CR

9        102           253    29          CR

 

So i dont need 4th and 8 th observation in the output. I am trying to use lag function but its not working properly. Could you please send me the piece of code to get the same output as above. Thanks in Advance.

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

Try next code:

data want;
 set have;
    by subject Avalc notsorted ;
        if  first.avalc or last.avalc then output;
run;

View solution in original post

5 REPLIES 5
LeonidBatkhan
Lapis Lazuli | Level 10

Hi mounikag,

 

If you need to throw out some observations by their numbers, you can do it this way:

data WANT;
   set HAVE;
   if _n_ in (4,8) then delete;
run;

Hope this helps.

mounikag
Obsidian | Level 7

Hi Leonid,

 

Thanks for your reply . I can't delete by using the observations because i have like 1000 of observations . Sorry if you misunderstood my question . If data has three immediate consecutive avalc values it should display first and last aval . So i mean if the data has 

DAta:

PR 

CR 

CR 

SD 

SD 

SD 

So i should get the output has :

PR 

CR 

SD 

SD

Shmuel
Garnet | Level 18

Try next code:

data want;
 set have;
    by subject Avalc notsorted ;
        if  first.avalc or last.avalc then output;
run;
mounikag
Obsidian | Level 7
Hi Shmuel,

Your response is helpful. Thanks.

Regards,
Mounika
ballardw
Super User

@mounikag wrote:

Hi All,

 

I need help in programming part. I have dataset :

 

Dataset:

Obs  Subject    ADY   Avisitn    Avalc  

1       101           86      12            PR

2        101           186     17          PD

3        101           257     24          PR 

4        101           327     29          PR

5        101           382     32          PR

6        102           76       14          PR

7        102          128     17          CR

8        102          188     24          CR 

9        102           253    29          CR

 

I need a output these way:

 

Obs  Subject    ADY   Avisitn    Avalc  

1       101           86      12            PR

2        101           186     17          PD

3        101           257     24          PR 

5        101           382     32          PR

6        102           76       14          PR

7        102          128     17          CR

9        102           253    29          CR

 

So i dont need 4th and 8 th observation in the output. I am trying to use lag function but its not working properly. Could you please send me the piece of code to get the same output as above. Thanks in Advance.

 


What is the RULE involved with why the 4th and 8th observation are to be removed? A value? A combination of values? A count of rows within subject? You just don't like 4 and 8? A specific value on the previous row?

 

It is very easy to create a solution that will remove those two specific records but that quick and easy solution is almost certainly not going to work for a general case in data sets with 1000's of records where you want to remove 100's of records.

 

 

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
  • 507 views
  • 0 likes
  • 4 in conversation