I have below dataset
data birthdate;
input name $ DoB date9. ;
format DoB date9.;
datalines;
naomi 19-Nov-2004
pixxa 19-Nov-2006
katty 10-Nov-2009
Sneha 19-Nov-2006
Aron 30-Oct-2009
Snaya 19-Feb-2006
Army 19-May-2009
Page 20-Feb-1982
Suze 11-Sep-1993
Taity 30-Oct-2009
Sainaa 19-Feb-2006
;
run;
I want to create a macro where for a given date value , it will check for first 5 records and if it matches it sends "happy birthday <person name> ".
it means if DOB =19-Nov-2006 then it will send happy bithday to both pixxa and sneha.
Please help.
What do yo mean by "send"?
This outputs the messages:
data birthdate;
input name $ DoB :date11.;
format DoB date9.;
datalines;
naomi 19-Nov-2004
pixxa 19-Nov-2006
katty 10-Nov-2009
Sneha 19-Nov-2006
Aron 30-Oct-2009
Snaya 19-Feb-2006
Army 19-May-2009
Page 20-Feb-1982
Suze 11-Sep-1993
Taity 30-Oct-2009
Sainaa 19-Feb-2006
;
proc sql;
select "Happy birthday " !! name
from birthdate (obs=5)
group by dob
having count(*) > 1
;
quit;
Note that your informat as posted is too short, the DOB strings are 11 characters long; therefore, with your code, all years would be either 2019 or 2020.
@Aexor wrote:
I have below dataset
data birthdate;
input name $ DoB date9. ;
format DoB date9.;
datalines;naomi 19-Nov-2004
pixxa 19-Nov-2006
katty 10-Nov-2009
Sneha 19-Nov-2006
Aron 30-Oct-2009
Snaya 19-Feb-2006
Army 19-May-2009
Page 20-Feb-1982
Suze 11-Sep-1993
Taity 30-Oct-2009
Sainaa 19-Feb-2006
;
run;
I want to create a macro where for a given date value , it will check for first 5 records and if it matches it sends "happy birthday <person name> ".
it means if DOB =19-Nov-2006 then it will send happy bithday to both pixxa and sneha.
Please help.
What do yo mean by "send"?
This outputs the messages:
data birthdate;
input name $ DoB :date11.;
format DoB date9.;
datalines;
naomi 19-Nov-2004
pixxa 19-Nov-2006
katty 10-Nov-2009
Sneha 19-Nov-2006
Aron 30-Oct-2009
Snaya 19-Feb-2006
Army 19-May-2009
Page 20-Feb-1982
Suze 11-Sep-1993
Taity 30-Oct-2009
Sainaa 19-Feb-2006
;
proc sql;
select "Happy birthday " !! name
from birthdate (obs=5)
group by dob
having count(*) > 1
;
quit;
Note that your informat as posted is too short, the DOB strings are 11 characters long; therefore, with your code, all years would be either 2019 or 2020.
@Aexor wrote:
I have below dataset
data birthdate;
input name $ DoB date9. ;
format DoB date9.;
datalines;naomi 19-Nov-2004
pixxa 19-Nov-2006
katty 10-Nov-2009
Sneha 19-Nov-2006
Aron 30-Oct-2009
Snaya 19-Feb-2006
Army 19-May-2009
Page 20-Feb-1982
Suze 11-Sep-1993
Taity 30-Oct-2009
Sainaa 19-Feb-2006
;
run;
I want to create a macro where for a given date value , it will check for first 5 records and if it matches it sends "happy birthday <person name> ".
it means if DOB =19-Nov-2006 then it will send happy bithday to both pixxa and sneha.
Please help.
That's because you abuse macro language for something it can't do. Macro statements do not have access to data step variables, as the macro statements are resolved before the data step is even compiled.
The macro PREprocessor is a code generator that helps you in writing dynamic or repeating code, it does NOT work with data.
Your macro creates this code (exactly this code):
data birth;
set birthdate;
where DOB="19-Nov-2004"d ;
where DOB="19-Nov-2004"d ;
and, while doing that, puts these messages to the log:
"Happy birthday" || name "Happy birthday" || name
Then the data step runs, because SAS Studio or Enterprise Guide helps you out by providing a RUN statement, and creates a dataset according to the WHERE condition (only the second, as it replaces the first; a NOTE about that is also found in the log)
You need to use data step code, and if I read your intentions right, you want the occurrences between &i and &n to appear in the output dataset:
%macro birthmsg(date,i,n);
data birth;
set birthdate;
where DOB="&date."d;
counter + 1;
if &i. le counter le &n.;
message = "Happy birthday" || name;
run;
%mend birthmsg;
%birthmsg(19-Nov-2006,1,2)
I changed the year to 2006, as there's only one date in 2004
This is bogus. You are still misusing the macro language, and you should STUDY THE LOG!!!
The macro creates this code:
data birth;
set birthdate;
where DOB="&date"d ;
z = cats("Happy birthday", name);
where DOB="&date"d ;
z = cats("Happy birthday", name);
where DOB="&date"d ;
z = cats("Happy birthday", name);
where DOB="&date"d ;
z = cats("Happy birthday", name);
where DOB="&date"d ;
z = cats("Happy birthday", name);
just repeating the same statements for NO reason.
OMIT the macro loop entirely, it solves no purpose.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.