DATA Step, Macro, Functions and more

Name Column as previous month

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Name Column as previous month

Hello,

 

I am trying to name a column as the previous month, with the year if possible, but at this point just the month would be a big help. 

Below is the working code I have, however, this only names the column the current month. 

 

I've tried INTNX but to no avail. 

 

PROC SQL;
CREATE TABLE MAP_VALIDATION AS
SELECT DISTINCT t1.'MAP_ID'n,
(COUNT(t1.MPXN)) AS %sysfunc(today(),monyy7.)
FROM ZZ_PRC_OUTPUT t1
GROUP BY t1.'MAP_ID'n;

QUIT;

 

Any help would be MUCH appreciated. 


Accepted Solutions
Solution
4 weeks ago
Super User
Super User
Posts: 9,599

Re: Name Column as previous month

Posted in reply to Sassy-Grrl

I highly recommend you don't.  Column names are something for a programming point of view, not for a user looking at the data.  As you have found, just creating the variable like that is a real pain, now imagine trying to program with that, each datastep you need to find out what variables are present, what they are called etc.  It will just make all programming far more complicated and less robust.  If you need the information for a report, then put this information in the Label - which is the part for free text to show what the variable contains, and give the variable a logical simple name.

I would also highly recommend not to use named literals:

t1.'MAP_ID'n,

These are really only there for the rare time that you need to access a non-standard variable name from a bad data source such as Excel.  Normal programming should use SAS compliant variable names - as you will have been shown through all basic courses - to make coding far simpler, easier to read, and more robust.

Please avoid shouting code also.

data _null_;
call symputx('my',put(intnx('month',today(),1),monyy7.));
run;

proc sql; create table map_validation as select distinct t1.map_id, count(t1.mpxn) as &my. from zz_prc_output t1 group by t1.map_id; quit;

I tend to do things like that in a datastep as the syntax is easier to read than a whole stream of % and &'s.

View solution in original post


All Replies
Solution
4 weeks ago
Super User
Super User
Posts: 9,599

Re: Name Column as previous month

Posted in reply to Sassy-Grrl

I highly recommend you don't.  Column names are something for a programming point of view, not for a user looking at the data.  As you have found, just creating the variable like that is a real pain, now imagine trying to program with that, each datastep you need to find out what variables are present, what they are called etc.  It will just make all programming far more complicated and less robust.  If you need the information for a report, then put this information in the Label - which is the part for free text to show what the variable contains, and give the variable a logical simple name.

I would also highly recommend not to use named literals:

t1.'MAP_ID'n,

These are really only there for the rare time that you need to access a non-standard variable name from a bad data source such as Excel.  Normal programming should use SAS compliant variable names - as you will have been shown through all basic courses - to make coding far simpler, easier to read, and more robust.

Please avoid shouting code also.

data _null_;
call symputx('my',put(intnx('month',today(),1),monyy7.));
run;

proc sql; create table map_validation as select distinct t1.map_id, count(t1.mpxn) as &my. from zz_prc_output t1 group by t1.map_id; quit;

I tend to do things like that in a datastep as the syntax is easier to read than a whole stream of % and &'s.

New Contributor
Posts: 2

Re: Name Column as previous month

Wow, thank you so much for that quick reply.
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 83 views
  • 2 likes
  • 2 in conversation