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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

4 REPLIES 4
kiranv_
Rhodochrosite | Level 12

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' )

 

Reeza
Super User

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;

 

 

Tom
Super User Tom
Super User

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;
ballardw
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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