Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More Efficient Way of Writing If-Then/Else

Status
Not open for further replies.

ks01

MIS
Aug 11, 2002
110
US
Hi everyone ...

I'd like to ask what I think is simple. I'm still learning SAS and am trying to clean up some "wordy" code. I'm looking for input from others on way that I can go about the same thing, but more efficiently. For example, here is a simple If-Then/Else.

My objective; I'm looking to take the READTIME variable from my flightdata data set and I only want to see observations where the READTIME happened yesterday, regardless of what yesterday is.

Code:
data temp1;
  set mylib.flightdata;
  yesterday = today() - 1;
  readdate_dpt = datepart(readtime);
  if readdate_dpt = yesterday then output;
    else delete;
run;

My code works perfectly fine, I'm just interested if anyone has any feedback on efficiency.

Would appreciate any feedback anyone has.

Thanks!

Kent
 
ks01,
I'm no expert, but you can trim down the code by using a where statement.

Code:
data temp1;
	set mylib.flightdata;
where datepart(date) = today()-1;
run;

I didn't do any compute time comparisons, but it seems like it would run more efficiently since it has one less calculation.

~dblan
 
You can also include the where statement as a dataset option...
Code:
data temp1;
    set mylib.flightdata(where=(datepart(date)=today()-1));
run;
I always thought this was more efficient than using Where in the datastep, but I'm not 100% sure that it's more efficient. It's definitely not less.
If you already have the datepart as a separate variable, then use that as that'll be more efficient as it won't need to do the calc for every record in this step.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Thanks dblan and Chris. Both of your explanations have helped.

I'd like to ask Chris for further clarifications on including the WHERE statement in the SET command, how would I go about including this when I have already specified the KEEP option? I have tried:

Code:
 SET MYLIB.MONDAY ((WHERE=DATEPART(READTIME)=TODAY()-1) (KEEP=JOB JESNR SYSTEM SRVCLASS RACFUSER READTIME CPUTM WAIT PROCESS TOTAL YESTERDAY2));

But my log hiccups with:

Code:
15           DATA TEMP (KEEP=JOB JESNR SYSTEM SRVCLASS RACFUSER READTIME CPUTM WAIT PROCESS TOTAL
15       !  YESTERDAY2);
16             SET MYLIB.MONDAY ((WHERE=DATEPART(READTIME)=TODAY()-1) (KEEP=JOB JESNR SYSTEM
                                 _      ________                      _
                                 22     23                            22
                                                                      76
ERROR 22-7: Invalid option name (.

ERROR 23-7: Invalid value for the WHERE option.

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, ;, END, 
              KEY, KEYS, NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_.  

ERROR 76-322: Syntax error, statement will be ignored.

16       !     SET MYLIB.MONDAY ((WHERE=DATEPART(READTIME)=TODAY()-1) (KEEP=JOB JESNR SYSTEM
                                                          _
                                                          22
ERROR 22-7: Invalid option name =.

16       !     SET MYLIB.MONDAY ((WHERE=DATEPART(READTIME)=TODAY()-1) (KEEP=JOB JESNR SYSTEM
                                                                  _
                                                                  22
16       ! SRVCLASS RACFUSER READTIME CPUTM WAIT PROCESS TOTAL YESTERDAY2)) ;
ERROR: The WHERE data set option requires the where expression to be enclosed by parentheses.
ERROR 22-7: Invalid option name -.

Is it bad parentheses matching on my part?

Thanks ...

Kent
 
It's like this:-
Code:
SET MYLIB.MONDAY(WHERE=(DATEPART(READTIME)=TODAY()-1)
                  KEEP=JOB JESNR SYSTEM SRVCLASS RACFUSER READTIME CPUTM WAIT PROCESS TOTAL YESTERDAY2);

That's one thing that always threw me in the doco, there's examples of each option, but very few examples of multiple options and how they should be combined. :)

I hope that this helps.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top