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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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