BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi. I'm trying to get about 115 observations from a data set that has 10000. I want all the information for each of the 115 observations. I tried this the code below and also used an 'or' instead of the 'and'. But, I can't get a dataset with just the 115 observations. How can I do so? Thank you.

data schaudit;
set sch;
if schcode =

0100610 and
0107292 and
0109785 and
0113514 and
0114371 and
0115089 and
0115105 and
0116129 and
....i HAVE ALL 115 SCHCODES LISTED...
run; Message was edited by: jcis
6 REPLIES 6
LinusH
Tourmaline | Level 20
Well, the and does not work at all for your case, but or should, or more convenient in(0100610 ,0107292 , ...). Even more convenient, have your subset data driven by store your subset schcodes in a separate table:

proc sql;
create table schaudit as
select *
from sch
where schcode in(select schcode from schcodeTable)
;
quit;

Is schcode numeric or character? Your code imply numeric, but leading zeroes will be truncated automatically. And what didnt work when using OR? Please show relevant parts from your log. Are you certain that your 115 all exist in your data?

/Linus
Data never sleeps
Cynthia_sas
SAS Super FREQ
Hi:
The syntax of an IF (whether subsetting or with a 'then do") statement is very specific. You cannot string "OR" values/conditions together like this:[pre]
if somevar = 111 or 222 or 333 or 444 ....
[/pre]

Even though some languages might let you do this, SAS will not. Your condition must consist of expressions and logical operators. "or 222" is a valid operator (OR) followed by the constant 222 which is not a valid expression, if what you want to test is whether SOMEVAR=222. Here are some examples:
[pre]
IF somevar = 111 or somevar = 222 or somevar = 333 ....

IF somevar in (111,222,333,444) ....

IF something in ('value', 'value2', 'value3') ....

IF (something='value' AND another='value2')
OR
(somevar in (111,222)) ....
[/pre]

The other approach is to have a lookup table. At some sites, however, you are not allowed to use lookup tables because all the observations being selected must be auditable from within a single program. So, if you do need to use a subsetting IF or an IF with an OUTPUT statement, then your IF statement must follow the rules above.

cynthia
deleted_user
Not applicable
Thank you very much! It worked!
deleted_user
Not applicable
How do you use the lookup table? I have created a sas datable with
just the codes I want to select for. Then, I have the table I want to select the observations from based on select codes and update it in the Master datable.
Thanks.
LinusH
Tourmaline | Level 20
The easiest is to use SQL with a sub-query or an inner join:

Proc SQL;
create table schaudit as
select *
from sch
where schcode in (select schcodes from sch_lookUpTable)
;
create table schaudit as
select sch.*
from sch inner join
sch_lookUpTable
on sch.schcode eq sch_lookUpTable.schcode
;
quit;

/Linus
Data never sleeps
deleted_user
Not applicable
Thanks! I am new to Proc SQL and I love it. Appreciate your help.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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