DATA Step, Macro, Functions and more

%macro?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

%macro?

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;

Accepted Solutions
Solution
‎03-07-2018 01:33 PM
Super User
Posts: 13,358

Re: %macro?


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


All Replies
Super User
Posts: 23,343

Re: %macro?

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;


Solution
‎03-07-2018 01:33 PM
Super User
Posts: 13,358

Re: %macro?


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.

 

 

Occasional Contributor
Posts: 10

Re: %macro?

Thank you for your explanation ballardw. I understand my mistake and happy with your solution.

thank you
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 127 views
  • 1 like
  • 3 in conversation