Drop / delete certain attributes

Accepted Solution Solved
Reply
Contributor
Posts: 58
Accepted Solution

Drop / delete certain attributes

Hi I simply want to keep only data with centain qualities.

 Can somebody spot the mistake?

 

I don't understand why my code gives me a totally wrong output.

Data Kea.Trails1b(COMPRESS = YES REUSE = YES);  
length DIVISION_COUNTY_NME $30; 
format DIVISION_COUNTY_NME $30.; 
If DIVISION_COUNTY_NME in ('Denver','Douglas','Arapahoe', 'Jefferson', 'Adams', 'Bromfield', 'Elbert', 'Park', 
'Clear Creek','Gilpin', 'Larimer' )
THEN COUNTY = "yes" ;ELSE COUNTY = "no";
Set  Kea.Trails1a;
run;

I read it is easier to introduce a new variable and then delete the records with that specific variable pattern.

So here I want to keep records with the Counties:  'Denver','Douglas','Arapahoe', 'Jefferson', 'Adams', 'Bromfield', 'Elbert', 'Park',
'Clear Creek','Gilpin', 'Larimer' they shoud all become COUNTY = "yes" ;

 

1.JPG

Why is this not happening?? Please see screenshot below.


Accepted Solutions
Solution
‎07-02-2017 04:50 PM
Super User
Posts: 19,146

Re: Drop / delete certain attributes

Order of operations - move your SET statement up to either right after your DATA statement or after the LENGTH/FORMAT statements if you need to explicitly define those before you read in the data.

 

Data Kea.Trails1b(COMPRESS = YES REUSE = YES);
  
Set  Kea.Trails1a;

length DIVISION_COUNTY_NME $30; 
format DIVISION_COUNTY_NME $30.; 

If DIVISION_COUNTY_NME in ('Denver','Douglas','Arapahoe', 'Jefferson', 'Adams', 'Bromfield', 'Elbert', 'Park', 
'Clear Creek','Gilpin', 'Larimer' )
THEN COUNTY = "yes" ;
ELSE COUNTY = "no";

run;

 

 

View solution in original post


All Replies
PROC Star
Posts: 283

Re: Drop / delete certain attributes

try trim, see whether it will bring any change

 

If trim(DIVISION_COUNTY_NME) in ('Denver','Douglas','Arapahoe', 'Jefferson', 'Adams', 'Bromfield', 'Elbert', 'Park', 
'Clear Creek','Gilpin', 'Larimer' )

 

Solution
‎07-02-2017 04:50 PM
Super User
Posts: 19,146

Re: Drop / delete certain attributes

Order of operations - move your SET statement up to either right after your DATA statement or after the LENGTH/FORMAT statements if you need to explicitly define those before you read in the data.

 

Data Kea.Trails1b(COMPRESS = YES REUSE = YES);
  
Set  Kea.Trails1a;

length DIVISION_COUNTY_NME $30; 
format DIVISION_COUNTY_NME $30.; 

If DIVISION_COUNTY_NME in ('Denver','Douglas','Arapahoe', 'Jefferson', 'Adams', 'Bromfield', 'Elbert', 'Park', 
'Clear Creek','Gilpin', 'Larimer' )
THEN COUNTY = "yes" ;
ELSE COUNTY = "no";

run;

 

 

Super User
Super User
Posts: 6,845

Re: Drop / delete certain attributes

The SET statement is an EXECUTED statement, not just something that is used during the compilation of the data step.  Where you place it in your program makes a difference.

 

You are creating the new variable COUNTY based on the values of DIVISION_COUNTY_NME from the previous observation.

Try this little program.

data test;
   length new_name name $30 ;
   new_name = name ;
   set sashelp.class ;
run;
proc print;
  var name new_name ;
run;
Super User
Posts: 11,130

Re: Drop / delete certain attributes

Please see this example code for what was happening based on similar logic. The second data step shows the value that was compared and may help you understand why the second Adams was marked correctly.

data example;
   input letter $;
datalines;
a
b
c
d
e
f
;
run;

data example2;
   if letter in ('a','c') then found='yes';
   lv = letter;
   set example;
   label lv='Actual value of letter compared';
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 121 views
  • 3 likes
  • 5 in conversation