Hi Team,
I created a macro variable
%let monthyear=Aug1989
I am trying to use:
data want;
set have;
year="&monthyear";
run;
The year variable has blank values. Please correct me
Thanks
%let monthyear=Aug1989;
/*This works*/
data want;
year="&monthyear";
put year=;
run;
/*This not. So you must have a variable named 'year' in table 'have' that is numeric*/
data want;
year=0;
year="&monthyear";
put year=;
run;
Year is defaulting to numeric. Since Monthyear is character then the assignment results in missing.
You need
length year $ 7;
before the assignment statement.
hi,
thanks for the reply.
u mean any macrovariable which contains a year in it is defaulted to NUMERIC??
Thanks
Hi,
Macro variables resolve to text. They resolve to SAS code.
So when you use:
%let monthyear=Aug1989 data want; set have; year="&monthyear"; run;
That should be the same as:
data want; set have; year="Aug1989"; run;
Often it's easier to debug the SAS code before jumping up a level of abstraction to debug the macro code.
If the SAS code isn't working, the macro language isn't the problem, or the solution.
HTH,
-Q.
Hi,
Thanks for the replies.
I have one final question on this. If i want the year as numeric variable(useful for sorting purposes instead of alphabetic months-years!!)
Do i need to create a fresh new variable from the obtained charecter variable and there is no other way to get it directly??
monthyear=08-1989;
data want;
set have;
year="&monthyear";
year_num=input(month_year,best.); /*if i do this i am getting as invalid argument to function input at line....and limit set by errors!!!!*/
run;
Thanks
When dealing with dates look to Date informats.
Leaving you older macro definition for monthyear
%let monthyear=Aug1999;
data want;
year_num= year(input("&monyear",monyy7.));
or if you want to change to mm-yyyy
year_num = year(input(&monyear",anydtdte.));
You were probably getting errors because the variable month_year didn't exist in your data set and wasn't referencing the macro variable that was the topic of your question, &monthyear. Assuming the %let just didn't get posted.
Hi Ballard,
as u said i dont have the month_year in my dataset. it is only a macro variable which is holding the value AUG1989
i want to create a new variable in my dataset with all the records holding the value in the macro variable
And SEP will have SEP1989 and so on
later I want to sort by the MONYYYY variable and NOT GET AUGUST AFTER APRIL
Thanks
Robert -
There are a lot of advantages to using SAS dates. You can sort the dates chronologically and use the formats to deal with output.
---------------------------------------
%macro one;
/* Set the macro variable. */
%let myDate = Sep1999;
/* Create the data set. */
data alpha;
/* Add the macro variable to the dataset. */
_date0 = "&mydate";
/* Covert the macro variable to a SAS date. */
_date1 = input(_date0,monyy7.);
/* Once it's a SAS date, we can output it any way we need */
/* using the SAS formats. Using SAS date, you can sort */
/* dates knowing they'll be in chronological order. You */
/* just need to know the right format when it comes time */
/* to display the output. */
put "Month/Year: " _date1 mmyys7.;
put "Name of the month: " _date1 monname9.;
put "Day of the week + date: " _date1 weekdate29.;
run;
%mend one;
%one;
---------------------------------------
The results of the above code are:
Month/Year: 09/1999
Name of the month: September
Day of the week + date: Wednesday, September 1, 1999
---------------------------------------
I hope it helps.
Thanks for the reply. That was of great help
what is the equalant format of mmyys7. to get year first and month next like: 1999/09
because for sorting purposes i beleive year has to be at the first otherwise
11-2013
11-2013
11-2012 /*NOV OF 2012 is sorting with NOV 0f 2013 which we dont want since the first digits are 11(WOULD IT NOT)*/
10-2012
09-2013
08-2013
07-2013;
OR THAT HAPPEND IF THE DATE IS CHARECTER VARIABLE AND WE DONT NEED TO WORRY SINCE WE HAVE A NUMERIC DATE?????
Thanks
Just swap the MMs and the YYs. - yymms7.
I've modified my code to show the same process with sorting and more months.
-----------------------------
%macro one;
/* -------------------------------------------- */
/* Set the macro variables. */
%let myDate1 = Sep1999;
%let myDate2 = Sep2000;
%let myDate3 = Dec1999;
%let myDate4 = Dec2000;
/* -------------------------------------------- */
/* Create the dataset with the text dates. */
data alpha;
%do i = 1 %to 4;
_date0 = "&&mydate&i";
output;
%end;
run;
/* -------------------------------------------- */
/* Convert the text dates to SAS dates. */
data beta;
set alpha;
_date1 = input(_date0,monyy7.);
run;
/* -------------------------------------------- */
/* Sort on the SAS dates. */
proc sort data=beta out=gamma;
by _date1;
run;
/* -------------------------------------------- */
/* Output */
data _null_;
set gamma;
put "* * * * * * * * * * * * * * * * * * * * *";
put "SAS Date: " _date1;
put "Month/Year: " _date1 mmyys7.;
put "Year/Month: " _date1 yymms7.;
put "Name of the month: " _date1 monname9.;
put "Day of the week + date: " _date1 weekdate29.;
run;
%mend one;
%one;
----------------------------
The results are:
* * * * * * * * * * * * * * * * * * * * *
SAS Date: 14488
Month/Year: 09/1999
Year/Month: 1999/09
Name of the month: September
Day of the week + date: Wednesday, September 1, 1999
* * * * * * * * * * * * * * * * * * * * *
SAS Date: 14579
Month/Year: 12/1999
Year/Month: 1999/12
Name of the month: December
Day of the week + date: Wednesday, December 1, 1999
* * * * * * * * * * * * * * * * * * * * *
SAS Date: 14854
Month/Year: 09/2000
Year/Month: 2000/09
Name of the month: September
Day of the week + date: Friday, September 1, 2000
* * * * * * * * * * * * * * * * * * * * *
SAS Date: 14945
Month/Year: 12/2000
Year/Month: 2000/12
Name of the month: December
Day of the week + date: Friday, December 1, 2000
Good luck.
Thanks and well said
i understand it better now...
Thanks
%let monthyear=Aug1989;
/*This works*/
data want;
year="&monthyear";
put year=;
run;
/*This not. So you must have a variable named 'year' in table 'have' that is numeric*/
data want;
year=0;
year="&monthyear";
put year=;
run;
hi,
I tried The first one and doesnt work for me. I dont know why??
Also i dont have a variable called Year in my table at all!!!!!!
Thanks
Hmm, Have you tried restarting your SAS session?
nooo...not since morning.I will try that too.
Thanks
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.