BookmarkSubscribeRSS Feed
Adityak204
Calcite | Level 5

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

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Astounding
PROC Star

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

 

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

Adityak204
Calcite | Level 5

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

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

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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 5 replies
  • 3463 views
  • 2 likes
  • 5 in conversation