BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AJ_Brien
Quartz | Level 8
Hello mentors,

I have a base dataset that has the following columns: _name_, 201902, 201903............202110.
I’m looking to extract the 1st and last columns, so I defined a macro variable:
%let yyyymm= %sysfunc(intnx(month,%sysfunc(date()),-1,s, yymmn6.);
Which gives me the value 202110.

So now i try to create a dataset:
Data want (keep= _name_ &yyyymm.);
Set have;
Run;

This keeps giving me errors:
Variable name “202110” is not valid.
Invalid value for the keep option.

I even did proc contents for ‘have’ dataset, and 202110 is a valid variable in there.
What am I doing wrong?

Thank you for all the help!
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

There are rules that determine what names are allowed. Normally, a variable name of 202110 is not allowed (and really is a bad idea to have calendar information in variable names anyway).

 

Either you are seeing the variable label when you look at the data set (are you?) or the data set was created with

options validvarname=any;

in which case you can refer to a variable name as '202110'n (note the quotes and the suffix which is the letter n) and so you can refer to it as

 

Data want (keep= _name_ "&yyyymm"n);

Do yourself a favor and turn this data set into a long data set (instead of a wide data set) where the month is the value of the variable named MONTH, and then almost all of your programming becomes much much much much much much much much easier muchly.

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

There are rules that determine what names are allowed. Normally, a variable name of 202110 is not allowed (and really is a bad idea to have calendar information in variable names anyway).

 

Either you are seeing the variable label when you look at the data set (are you?) or the data set was created with

options validvarname=any;

in which case you can refer to a variable name as '202110'n (note the quotes and the suffix which is the letter n) and so you can refer to it as

 

Data want (keep= _name_ "&yyyymm"n);

Do yourself a favor and turn this data set into a long data set (instead of a wide data set) where the month is the value of the variable named MONTH, and then almost all of your programming becomes much much much much much much much much easier muchly.

--
Paige Miller
AJ_Brien
Quartz | Level 8
That worked! I added ‘n’ as you suggested.
Thank you so much!
AJ_Brien
Quartz | Level 8
Someone else is creating the base dataset so I couldn’t update the variable names. But adding ‘n’ After the variable name totally worked. Thank you so much!
PaigeMiller
Diamond | Level 26

Let me correct something you said.

 


@AJ_Brien wrote:
Someone else is creating the base dataset so I couldn’t update the variable names. But adding ‘n’ After the variable name totally worked. 

you don't add 'n' with quotes

 

you put the variable names in quotes and then append the letter n

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 4 replies
  • 1310 views
  • 0 likes
  • 2 in conversation