Write and run SAS programs in your web browser

Adding date in variable name in SAS

Reply
New Contributor
Posts: 2

Adding date in variable name in SAS

I have a column with name total transaction. I want to add a date 4 days back from now in its name .

 

For example if today is 20161220 so I want my variable to be renamed as total_transaction_20161216.

 

n general purpose while working in SAS i use intnx(date, today(), -4) for getting a date 4 days back from now, but i do not know how to add it in variable name.

 

Please suggest me a way out of my problem. Thanks

Super User
Super User
Posts: 7,682

Re: Adding date in variable name in SAS

I would suggest to not do this.  Variable names are for programming purposes only, you will want to keep varnames short, succient, and easy to program with.  If you name variables with <prefix><suffix> you can then array them, and refer to them generically.  Counter this with your suggestion where every programming task needs to know what the variable name is.  Far more complicated code, far less maintainable.  Now if you need the year to be displayed, then put year information in the label - you will almost never program with labels, so they can be anything, and it doesn't affect programming.  So use the right part of the variable for the right use:

Var name: total_transaction1

Var Label: Total Transaction for year 2016

 

In programming I can simply use total_transaction: to refer to all these transactions, but still display the required information.

Super User
Posts: 5,353

Re: Adding date in variable name in SAS

[ Edited ]

Assuming you still want to do this, here's a way:

 

rename total_transaction = total_transaction_%sysfunc(putn(%sysfunc(today())-4, yymmddn8.));

New Contributor
Posts: 2

Re: Adding date in variable name in SAS

[ Edited ]

Hi! I tried alreay tried this code but its not working. It works well when i use it for manipulating data inside the column but not on renaming column... Thanks

Super User
Posts: 9,856

Re: Adding date in variable name in SAS

proc datasets library=work nolist nodetails;
modify have
rename total_transaction = total_transaction_%sysfunc(putn(%sysfunc(today())-4, yymmddn8.));
quit;
Super User
Posts: 11,105

Re: Adding date in variable name in SAS

Many times when we see variables named like this we see that they end up in a data set that keeps adding columns for multiple dates. And then questions become "how do I write something that allows me to dynamically manipulate this everchanging list of variables".

Typically the best approach is to have another variable that indicates the date of the value and the value on separate observations because then you can subset or select on the actual date and then sum / average/ other summary statistic based on the dates.

Ask a Question
Discussion stats
  • 5 replies
  • 254 views
  • 2 likes
  • 5 in conversation