BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fieldsa83
Quartz | Level 8

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?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

9 REPLIES 9
ballardw
Super User

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.

Reeza
Super User

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;

fieldsa83
Quartz | Level 8

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?
fieldsa83
Quartz | Level 8

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

data_null__
Jade | Level 19

@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.

fieldsa83
Quartz | Level 8

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

Reeza
Super User
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;
Reeza
Super User

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.

Shmuel
Garnet | Level 18

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.

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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