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

Hi!

 

I have a test data set. If there is more than one identifier in the table and it is included 20900101 date then keep it that row. The rest are related to this ids delete it/drop it.

 

data;
input id date$ var1$ var2$;
datalines;
1 20900101 abc abc2
2 20070102 cdf cdf2
2 20900101 jdk lao2
1 20110101 cds sxa
1 20150202 jui wsa
3 20900101 wer wes2 
1 21000101 rte www
4 20190101 www rrr 
;
run;

 

so i'd like to this: (1. id contains 20900101 2. id contains 20900101 3. id contains 20900101 4. id not contains so keep it)

data;
input id date$ var1$ var2$;
datalines;
1 20900101 abc abc2
2 20900101 jdk lao2
3 20900101 wer wes2 
4 20190101 www rrr 
;
run;

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
hashman
Ammonite | Level 13

@ger15xxhcker :

It looks like a typical job for the DoW-loop:

data test ;                              
  input id (date var1 var2) ($) ;        
  cards ;                                
1 20900101 abc abc2                      
2 20070102 cdf cdf2                      
2 20900101 jdk lao2                      
1 20110101 cds sxa                       
1 20150202 jui wsa                       
3 20900101 wer wes2                      
1 21000101 rte www                       
4 20190101 www rrr                       
;                                        
run ;                                    
                                         
proc sort data = test out = _test ;      
  by id ;                                
run ;                                    
                                         
data want (drop = _:) ;                  
  do _n_ = 1 by 1 until (last.id) ;      
    set _test ;                          
    by id ;                              
    if date ne "20900101" then continue ;
    output ;                             
    _flag = 1 ;                          
  end ;                                  
  do _n_ = 1 to _n_ ;                    
    set _test ;                          
    if not _flag then output ;           
  end ;                                  
run ;                                    

Kind regards

Paul D.

View solution in original post

3 REPLIES 3
hashman
Ammonite | Level 13

@ger15xxhcker :

It looks like a typical job for the DoW-loop:

data test ;                              
  input id (date var1 var2) ($) ;        
  cards ;                                
1 20900101 abc abc2                      
2 20070102 cdf cdf2                      
2 20900101 jdk lao2                      
1 20110101 cds sxa                       
1 20150202 jui wsa                       
3 20900101 wer wes2                      
1 21000101 rte www                       
4 20190101 www rrr                       
;                                        
run ;                                    
                                         
proc sort data = test out = _test ;      
  by id ;                                
run ;                                    
                                         
data want (drop = _:) ;                  
  do _n_ = 1 by 1 until (last.id) ;      
    set _test ;                          
    by id ;                              
    if date ne "20900101" then continue ;
    output ;                             
    _flag = 1 ;                          
  end ;                                  
  do _n_ = 1 to _n_ ;                    
    set _test ;                          
    if not _flag then output ;           
  end ;                                  
run ;                                    

Kind regards

Paul D.

ger15xxhcker
Quartz | Level 8

Thanks for your quick help!

Ksharp
Super User
data have;
input id date$ var1$ var2$;
datalines;
1 20900101 abc abc2
2 20070102 cdf cdf2
2 20900101 jdk lao2
1 20110101 cds sxa
1 20150202 jui wsa
3 20900101 wer wes2 
1 21000101 rte www
4 20190101 www rrr 
;
run;
proc sql;
create table want as
select *
 from (select * from have group by id having count(*)>1)
  where date='20900101'
union all
select * from have group by id having count(*)=1;
quit;

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
  • 3 replies
  • 487 views
  • 1 like
  • 3 in conversation