Help using Base SAS procedures

IF THEN

Reply
N/A
Posts: 0

IF THEN

Hello,

I have DATE as character variable (length 9) in the dataset "now_char" and the DATE column has dates like :- 22JAN2010 . I use the code:--

data combine;
set now_char;
if DATE LT "07JAN2010" THEN
Week = "Week1";
ELSE IF DATE GT "07/01/08" AND DATE LT "15/01Jan/08" THEN
Week = "Week2";
RUN;

Log:-

NOTE: Character values have been converted to numeric values at the places given by: (Line)Smiley SadColumn).
760:16 762:21 762:43
NOTE: Invalid numeric data, '27JAN2008' , at line 760 column 16.
NOTE: Invalid numeric data, '26/01/08' , at line 762 column 21.
NOTE: Invalid numeric data, '31/01Jan/08' , at line 762 column 43.

Any suggestions?

Kindest Regards,
Kriti
New Contributor
Posts: 2

Re: IF THEN

Hi,

The easiest solution is to convert the character data (which is just a piece of text) into a SAS date (the number of days since 01JAN1960). To do this you need something like:

data combine;
set now_char;
if input(date,date9.) lt "07JAN2010"d then week = "Week1";
else if ....;
run;

Note that the d following the date in quotes inputes the text as a SAS date ("07JAN2010"d). I would have wrote out the whole data step but the dates in the second category don't appear to make any sense.

Hope that helps
PROC Star
Posts: 7,366

Re: IF THEN

I'm not sure what you are trying to do. If it is simply to count the number of seven day periods since January 1 of a given year, you could use something like:

data combine;
set now_char;
week=intck(cats('week.',
weekday(mdy(1,1,year(input(date,date9.))))),
mdy(1,1,year(input(date,date9.))),
input(date,date9.))+1;
run;

HTH,
Art
Occasional Contributor tlt
Occasional Contributor
Posts: 11

Re: IF THEN

Hi,

Code and notes do not match
Notes say '27JAN2008' that can't be found in code.


NOTE: Character values have been converted to numeric values at the places given by: (Line)Smiley SadColumn) 760:16 762:21 762:43
Look in your log, if you see something like 760 if DATE LT "..." THEN
It means your DATE is not a character variable

HTH
PROC Star
Posts: 7,366

Re: IF THEN

While I still don't know what you are trying to accomplish, I hadn't payed enough attention to the notes you indicated were in the SAS log.

Since your dates have an odd mixture of formats, again only if I correctly understand the problem, you might try something like:

data now_char;
input date $11.;
cards;
27JAN2008
26/01/08
31/01Jan/08
;
options datestyle=dmy;
data combine;
set now_char;
if anyalpha(scan(date,2))*anydigit(scan(date,2)) then
date=compress(date,,"a");
format date2 date9.;
date2=input(date,anydtdte11.);
week=intck(cats('week.',
weekday(mdy(1,1,year(input(date,anydtdte11.))))),
mdy(1,1,year(input(date,anydtdte11.))),
input(date,anydtdte11.))+1;
run;

HTH,
Art
Valued Guide
Posts: 2,175

Re: IF THEN

Hi Kriti

That looks like code you have written, so just re-write it a little differently
"07JAN2010" is a character constant
"07JAN2010"d is a date constant
However, if your data "work.now_char" holds variable "date" as a character variable, your code will have to "interpret" it as a date to use relative tests like GT or LE..... not very different
First since you refer to "DATE" more than once, convert it to a sas date, just once like:
sas_date = input(DATE, date9.) ;

Your data step would adapt simply like:

> data combine;
> set now_char;
sas_date = input(DATE, date9.) ;

if sas_DATE LT "07JAN2010"d THEN

> Week = "Week1";

ELSE IF sas_DATE GT "07Jan08"d AND sas_DATE LT "15Jan08"d

> THEN
> Week = "Week2";
> RUN;

does that explain what you need?

peterC
Ask a Question
Discussion stats
  • 5 replies
  • 156 views
  • 0 likes
  • 5 in conversation