BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
unnati
Obsidian | Level 7

Question:- I would like to use Macro and want to create a new variable (i am trying to create age variable form birthday variable in dataset(excel file)) . I try to write this code. Can anyone explain what is wrong in this code and why i am not able to use Macro this way.

1.code 1
data fs;
set bb;
call symput('var',age(int((Today()-birthday)/365.25)));
run;

2. code2

data fs;
set bb;
call symput('var',age(int((&sysdate-birthday)/365.25)));
run;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@unnati wrote:

Question:- I would like to use Macro and want to create a new variable (i am trying to create age variable form birthday variable in dataset(excel file)) . I try to write this code.

 

Can anyone explain what is wrong in this code and why i am not able to use Macro this way.


Excel files do not show us the actual values in your data variables in the SAS data set.

 

Better is to post example data as a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

Why do you want a macro variable? How is it going to be used?

If you want an Age attached to each record in your data there is no reason to use a macro at all.

You can get the age given two SAS date values using

Age= yrdif(birthday,today(),'AGE'); which will yield a number of years and a decimal portion.

If you only want the calendar years then

Age = floor(yrdif(birthday,today,'AGE'));

which will discard the decimal portion.

 

 

View solution in original post

3 REPLIES 3
Reeza
Super User

What are you trying to do?

There's no AGE function so your reference there is incorrect.

 


@unnati wrote:

Question:- I would like to use Macro and want to create a new variable (i am trying to create age variable form birthday variable in dataset(excel file)) . I try to write this code. Can anyone explain what is wrong in this code and why i am not able to use Macro this way.

1.code 1
data fs;
set bb;
call symput('var',age(int((Today()-birthday)/365.25)));
run;

2. code2

data fs;
set bb;
call symput('var',age(int((&sysdate-birthday)/365.25)));
run;


ballardw
Super User

@unnati wrote:

Question:- I would like to use Macro and want to create a new variable (i am trying to create age variable form birthday variable in dataset(excel file)) . I try to write this code.

 

Can anyone explain what is wrong in this code and why i am not able to use Macro this way.


Excel files do not show us the actual values in your data variables in the SAS data set.

 

Better is to post example data as a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

Why do you want a macro variable? How is it going to be used?

If you want an Age attached to each record in your data there is no reason to use a macro at all.

You can get the age given two SAS date values using

Age= yrdif(birthday,today(),'AGE'); which will yield a number of years and a decimal portion.

If you only want the calendar years then

Age = floor(yrdif(birthday,today,'AGE'));

which will discard the decimal portion.

 

 

unnati
Obsidian | Level 7
Thank you for your explanation ballardw. I understand my mistake and happy with your solution.

thank you

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3 replies
  • 1793 views
  • 1 like
  • 3 in conversation