BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
kindbe17
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
%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.

--
Paige Miller

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26
%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.

--
Paige Miller
kindbe17
Fluorite | Level 6

Thank you so much! i got it

yabwon
Onyx | Level 15

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

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



PaigeMiller
Diamond | Level 26

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!

--
Paige Miller
kindbe17
Fluorite | Level 6

Dear Sir,

Thank you very much . this is a huge work you do for us.

AhmedAl_Attar
Ammonite | Level 13

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

 

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
AhmedAl_Attar
Ammonite | Level 13

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 😉 

kindbe17
Fluorite | Level 6

Thank you very-very much for explaining and givving me such a great example, i have already used it in my task 🙂

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1666 views
  • 3 likes
  • 4 in conversation