BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mona4u
Lapis Lazuli | Level 10

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

 

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

5 REPLIES 5
novinosrin
Tourmaline | Level 20

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

 

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;
mona4u
Lapis Lazuli | Level 10

It didn't work with me 

ballardw
Super User

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

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