Hi, who can help me how to shown below (Note that “1987 Salary” ) with macro variable, i am trying this
....%let n=1987. Salary;
data newone (rename =(_Name_=Statistic Col1=&n.));
set newdat1;
run;
but Sas tells me Variable name 1987. is not valid.
1987 Salary |
XX |
XX.X (XX.XX) |
XX.X |
XX, XX |
%let n=1987. Salary;
data newone (rename =(_Name_=Statistic Col1=&n.));
You can't have a period or a space in a SAS variable name. Your SAS variable names cannot begin with digits. On the other hand, the code below works:
%let n=Salary1987;
data newone (rename =(_Name_=Statistic Col1=&n.));
Although the above works, I think here we have a situation where calendar information is being placed into a variable name, which I think is almost always a poor choice. If you are looking for a column to identify values in a given year, you would be better off using PROC REPORT to generate this, rather than hard-coding the year into a variable name.
%let n=1987. Salary;
data newone (rename =(_Name_=Statistic Col1=&n.));
You can't have a period or a space in a SAS variable name. Your SAS variable names cannot begin with digits. On the other hand, the code below works:
%let n=Salary1987;
data newone (rename =(_Name_=Statistic Col1=&n.));
Although the above works, I think here we have a situation where calendar information is being placed into a variable name, which I think is almost always a poor choice. If you are looking for a column to identify values in a given year, you would be better off using PROC REPORT to generate this, rather than hard-coding the year into a variable name.
Thank you so much! i got it
In genaeral, as @PaigeMiller wrote SAS does not allow periods or spaces in variables name.
The general rule is: letters(roman alphabet, 26), underscore ("_") and digits (0-9) are only allowed in variables names. Plus: up to 32 characters and firs can by only letter or underscore.
The general rule is good programming practice in this case, but as you my expect there is also bad programming practice which involves option: validvarname, see the link to doc.: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lesysoptsref/p124dqdk8zoqu3n1r4nsfqu5vx52.htm
In code it would look like this:
data newdat1;
_Name_=1; Col1=2;
run;
OPTIONS VALIDVARNAME=ANY;
%let n='1987. Salary'n; /* <---------- text is in quotes and has "n" right after it, like the sas date literal has "d" [ '17jan2135'd ]*/
data newone (rename =(_Name_=Statistic Col1=&n.));
set newdat1;
run;
But as I wrote, it is bad programming practice.
Bart
So just to be clear, I agree with @yabwon, it is bad programming practice for the reason he states, and it is bad programming practice for the reason I stated. An example of double bad programming practice!
Dear Sir,
Thank you very much . this is a huge work you do for us.
Hi @kindbe17
One way to do this, if you don't want to go the Proc Report route as @PaigeMiller has suggested, would be
options validvarname=any;
%let n=1997 Salary;
data have;
col1=222;
run;
data want;
set have(rename=(col1="&n"n));
run;
proc print data=want noobs; run;
1997 Salary |
222 |
Of course, @AhmedAl_Attar is correct in the sense that this will work ... however, it is impossible for me to see how '1987. Salary'n is in any way a better or more useful variable name than Salary1987. It is, however a more difficult name to type, as extra characters are required and at least for me, typing quotes is a place where I make an inordinate number of typographical errors.
Now I said this isn't a better or more useful variable name, emphasis on variable name, but if you are creating a report, it is true that a properly worded column heading is a good thing to do, emphasis on column heading. Column headings in reports do not need to be the variable name. And so again, PROC REPORT seems to be a tool that allows this, without having to work extra hard to create and type variable names. In fact, having columns in PROC REPORT with calendar information in the column heading is a lot easier than trying to force DATA set variables to have the proper variable names.
I just wanted to confirm, I'm in total agreement with @PaigeMiller & @yabwon when it comes to variable names and naming strategies.
My approach was trying to showoff SAS's versatility and capability to match the flexibility other software packages and programs, namely MS Excel, offer their users.
But if I were to approach this as a pure SAS "developer", then I would 100% follow the SAS native variable naming rules, and assign variable label= attribute in order to hold a descriptive column header that can be displayed in SAS generated outputs (Report, Graph, listing...)
Just saying 😉
Thank you very-very much for explaining and givving me such a great example, i have already used it in my task 🙂
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 16. Read more here about why you should contribute and what is in it for you!
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.