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?
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;
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.
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;
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;
Nevermind, I figured out what you meant (did a cat("D",Sdate) in data step, then used sdate as idlabel in transpose).
@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.
Hmm not sure I understand where to put that statement..
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;
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.
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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.