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
Obs | complete_date |
1 | 29/05/2015 |
2 | 29/05/2015 |
3 | 10/6/2015 |
4 | 2/6/2015 |
5 | 10/6/2015 |
6 | 10/6/2015 |
7 | 10/6/2015 |
8 | 10/6/2015 |
9 | 2/7/2015 |
10 | 2/7/2015 |
11 | 6/7/2015 |
12 | 3/7/2015 |
13 | 18/11/2015 |
14 | 18/11/2015 |
15 | 18/11/2015 |
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;
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;
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;
It didn't work with me
Post the log of the data step. I bet there's a NOTE about a type conversion.
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).
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.
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.