DATA Step, Macro, Functions and more

scan function with if stetement

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 149
Accepted Solution

scan function with if stetement

Hi, 

I'm trying to use scan function with if statement but I guess I'm doing a mistake that prevents the variable count to count the observation that has this month. I'm not getting any error in the log but I'm not getting what I want 

 

here is my code 

%let todaysdate= %sysfunc(today(), DDMMYY10.);

%let thismonth=%scan(&todaysdate,2,"/");


data Asterias2; 
set Asterias1;

format complete_date ddmmyy10.;
if scan(complete_date,2,"/")=&thismonth then count=1;
run;

this is a sample of my data 

 

Obscomplete_date
129/05/2015
229/05/2015
310/6/2015
42/6/2015
510/6/2015
610/6/2015
710/6/2015
810/6/2015
92/7/2015
102/7/2015
116/7/2015
123/7/2015
1318/11/2015
1418/11/2015
1518/11/2015

 

 


Accepted Solutions
Solution
a month ago
Super User
Posts: 23,260

Re: scan function with if stetement

Posted in reply to novinosrin

 

if scan(complete_date,2,"/")

 

Complete_date is a SAS date, with a date format, it cannot be used with SCAN and it looks like you're trying to see if the months are the same between the two? complete_date is a number, such as 23554, so I'm not even sure what would get returned.

 

If so, I would use the MONTH() function to extract the month instead.

 

 

%let todaysdate= %sysfunc(today());

%let thismonth = %sysfunc(month(&todaysdate.));

data Asterias2; 
set Asterias1; 
 
format complete_date ddmmyy10.; 
if month(complete_date) = &thismonth. then  count=1; 
run;  

But you don't need macro variables at all for this. You could use:

 

if month(complete_date) = month(today()) then count=1;

View solution in original post


All Replies
PROC Star
Posts: 1,569

Re: scan function with if stetement

are you missing quotes around macro reference like?

 

data Asterias2; 
set Asterias1; 
 
format complete_date ddmmyy10.; 
if scan(complete_date,2,"/")="&thismonth" then  count=1; 
run;
Solution
a month ago
Super User
Posts: 23,260

Re: scan function with if stetement

Posted in reply to novinosrin

 

if scan(complete_date,2,"/")

 

Complete_date is a SAS date, with a date format, it cannot be used with SCAN and it looks like you're trying to see if the months are the same between the two? complete_date is a number, such as 23554, so I'm not even sure what would get returned.

 

If so, I would use the MONTH() function to extract the month instead.

 

 

%let todaysdate= %sysfunc(today());

%let thismonth = %sysfunc(month(&todaysdate.));

data Asterias2; 
set Asterias1; 
 
format complete_date ddmmyy10.; 
if month(complete_date) = &thismonth. then  count=1; 
run;  

But you don't need macro variables at all for this. You could use:

 

if month(complete_date) = month(today()) then count=1;
Frequent Contributor
Posts: 149

Re: scan function with if stetement

Posted in reply to novinosrin

It didn't work with me 

Super User
Posts: 9,880

Re: scan function with if stetement

Post the log of the data step. I bet there's a NOTE about a type conversion.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 13,304

Re: scan function with if stetement

When you use a DATE value with scan you get missing when scanning for the second value.

data _null_;
   complete_date= today();
   format complete_date ddmmyy10.;
   datathismonth = scan(complete_date,2,"/");
   put "data this month is: " datathismonth;
   scanresult = scan(complete_date,1);
   put scanresult=;
run;

You have to specifically either use the Put(datevalue,ddmmyy10) in the scan function OR

 

much better idea, use the proper function such as MONTH(complete_date).

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 184 views
  • 3 likes
  • 5 in conversation