DATA Step, Macro, Functions and more

Macro variable resolution

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,041
Accepted Solution

Macro variable resolution

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


Accepted Solutions
Solution
‎09-16-2013 03:54 PM
Respected Advisor
Posts: 3,156

Re: Macro variable resolution

Posted in reply to robertrao

%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;

View solution in original post


All Replies
Super User
Posts: 11,343

Re: Macro variable resolution

Posted in reply to robertrao

Year is defaulting to numeric. Since Monthyear is character then the assignment results in missing.

You need

length year $ 7;

before the assignment statement.

Super Contributor
Posts: 1,041

Re: Macro variable resolution

hi,

thanks for the reply.

u mean any macrovariable which contains a year in it is defaulted to NUMERIC??

Thanks

PROC Star
Posts: 1,322

Re: Macro variable resolution

Posted in reply to robertrao

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.

Super Contributor
Posts: 1,041

Re: Macro variable resolution

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

Super User
Posts: 11,343

Re: Macro variable resolution

Posted in reply to robertrao

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.

Super Contributor
Posts: 1,041

Re: Macro variable resolution

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

New Contributor
Posts: 4

Re: Macro variable resolution

Posted in reply to robertrao

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.

Super Contributor
Posts: 1,041

Re: Macro variable resolution

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

New Contributor
Posts: 4

Re: Macro variable resolution

Posted in reply to robertrao

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.

Super Contributor
Posts: 1,041

Re: Macro variable resolution

Thanks and well said

i understand it better now...

Thanks

Solution
‎09-16-2013 03:54 PM
Respected Advisor
Posts: 3,156

Re: Macro variable resolution

Posted in reply to robertrao

%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;

Super Contributor
Posts: 1,041

Re: Macro variable resolution

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

Respected Advisor
Posts: 3,156

Re: Macro variable resolution

Posted in reply to robertrao

Hmm, Have you tried restarting your SAS session?

Super Contributor
Posts: 1,041

Re: Macro variable resolution

nooo...not since morning.I will try that too.

Thanks

🔒 This topic is solved and locked.

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

Discussion stats
  • 16 replies
  • 420 views
  • 7 likes
  • 5 in conversation