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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

 


 

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

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.

 


 

Aexor
Lapis Lazuli | Level 10
Thank you so much! Can we do same thing in data step ?
Could you please check what went wrong here :

%macro birthmsg(date,i,n);
data birth;
set birthdate;
%do i =&i %to &n;
where DOB="&date"d ;
%put "Happy birthday" || name;
%end;
%mend birthmsg;

options symbolgen mprint;
%birthmsg(19-Nov-2004,1,2)

I am having doubt in how can we check for dates in macro variable?
Please help
Kurt_Bremser
Super User

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

Aexor
Lapis Lazuli | Level 10
Thanks for clarifying my doubts.
Though i updated the code to this and it worked too

%macro birthmsg(date,i,n);
data birth;
set birthdate;
%do i =&i %to &n;
where DOB="&date"d ;
z = cats("Happy birthday", name);
%end;
%mend birthmsg;

options symbolgen mprint;
%birthmsg(19-Nov-2006,1,5)
Kurt_Bremser
Super User

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.

sas-innovate-white.png

Register Today!

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.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 933 views
  • 3 likes
  • 2 in conversation