Using numeric variable name in macro (from transposed, using dates)

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

Using numeric variable name in macro (from transposed, using dates)

I transposed ID date so it's the column headers, so I have 201611 and then 201612.

 

I can do:

 

newvar = '201612'n - '201611'n

 

and it works.

 

However, I'm trying to use a macro:

 

%let firstdate = 201611

 

but i can't do:

 

newvar2 = '201612'n - &firstdate

 

since it treats &firstdate as an actual number. 

 

And i can't do

 

newvar2 = '201612'n - '&firstdate'n 

 

since it treats it literally.

 

How can I use a numeric variable name in a macro?

 

 


Accepted Solutions
Solution
‎01-03-2017 04:04 PM
Grand Advisor
Posts: 17,462

Re: Using numeric variable name in macro (from transposed, using dates)

Don't create variables that have numeric names, you're going to make more work for yourself than is necessary. Use IDLABEL in proc transpose to keep the labels you want, but create a new ID variable by appending a D in front of the YYYYMM and then you can do:

 

do i=D&firstDate -D&lastDate;

View solution in original post


All Replies
Grand Advisor
Posts: 10,251

Re: Using numeric variable name in macro (from transposed, using dates)

Macro values do not resolve within single quotes.

I might try "&firstdate"n 

 

But I would also suggest that you reevalute what you are doing as using values for variable names will complicate almost anything you are going to do further on. This is just one example.

Solution
‎01-03-2017 04:04 PM
Grand Advisor
Posts: 17,462

Re: Using numeric variable name in macro (from transposed, using dates)

Don't create variables that have numeric names, you're going to make more work for yourself than is necessary. Use IDLABEL in proc transpose to keep the labels you want, but create a new ID variable by appending a D in front of the YYYYMM and then you can do:

 

do i=D&firstDate -D&lastDate;

Frequent Contributor
Posts: 75

Re: Using numeric variable name in macro (from transposed, using dates)

Thanks, this looks like a strong solution. Could you clarify a bit, I'm not sure I fully know how to implement your suggestion.

 

To be clear I have 5 years worth of data, monthly: so it's 201101, 201102, 201103 ... 201612.

 

This is the current transpose:

 

 
PROC TRANSPOSE DATA=output.test
        OUT=output.transposedtable(drop=_NAME_ _LABEL_ LABEL="")
;
where industry ne .;
        BY prov industry;
        ID sdate;
        VAR FINALWT_Sum;
RUN;
 
Are you suggesting I create a do loop for an IDLABEL?
Frequent Contributor
Posts: 75

Re: Using numeric variable name in macro (from transposed, using dates)

Nevermind, I figured out what you meant (did a cat("D",Sdate) in data step, then used sdate as idlabel in transpose).

Respected Advisor
Posts: 3,776

Re: Using numeric variable name in macro (from transposed, using dates)


fieldsa83 wrote:

Nevermind, I figured out what you meant (did a cat("D",Sdate) in data step, then used sdate as idlabel in transpose).


No need for all that just use PREFIX=D the rest of your original code.

Frequent Contributor
Posts: 75

Re: Using numeric variable name in macro (from transposed, using dates)

Hmm not sure I understand where to put that statement..

Grand Advisor
Posts: 17,462

Re: Using numeric variable name in macro (from transposed, using dates)

PROC TRANSPOSE DATA=output.test
        OUT=output.transposedtable(drop=_NAME_ _LABEL_ LABEL="") prefix=D
;
where industry ne .;
        BY prov industry;
        ID sdate;
idlabel sdate;
        VAR FINALWT_Sum;
RUN;
Grand Advisor
Posts: 17,462

Re: Using numeric variable name in macro (from transposed, using dates)

My only suggestion would be to use CATT - it removes trailing blanks and does numeric to char conversion as necessary, so it can be helpful. I rarely use the CAT function at all.

Super User
Posts: 1,256

Re: Using numeric variable name in macro (from transposed, using dates)

Try each of next two possibilities:

 

1)  % let  firstdate = '201611'n;      /* as you want it to be a variable name */

 

OR

 

2)  newvar2 = '201612'n - " '&firstdate'"n   /* enclose with double quotes */

 

But, it will be better for future use and maintenance to rename variables

into:  V201611,  V201612 - just by adding an alpha character before the date.

 

 

☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 256 views
  • 2 likes
  • 5 in conversation