BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

What is the way to create macro variables base,next1,next2,next3,next4,next5,next6 with shorter code?

 

%let vector=2101+2102+2103+2104+2105+2106+2107;
%let n=%sysfunc(countw(&vector,'+')); %let base = %scan(&vector,0,’+’); %let Next1 = %scan(&vector,1,’+’); %let Next2 = %scan(&vector,2,’+’); %let Next3 = %scan(&vector,3,’+’); %let Next4 = %scan(&vector,4,’+’); %let Next5 = %scan(&vector,5,’+’); %let Next6 = %scan(&vector,6,’+’);

 

 

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

Do you want to create the vector macro variable smarter as well?

Ronein
Meteorite | Level 14

Let's start with this vector and create base and next1...next6 

Later it will be nice also to learn creating vector in smarter way

PeterClemmensen
Tourmaline | Level 20

I assume that you want to create the vector from some data, and you don't really need the vector macro variable anyways.

 

Try this

 

data have;
   do v = 2101 to 2107;
      output;
   end;
run;

proc sql noprint;
   select v into : next1 - 
   from have;
   %let n = &sqlobs.;
quit;

%put &n.;
%put &next1;
%put &&next&n;
Ronein
Meteorite | Level 14

Thank you,

What is the meaning of "Next1-"?

 

PeterClemmensen
Tourmaline | Level 20

Good question. Next1 is the name of the first macro variable that will be created. Since we do not (necessarily) know how many values are in the input data set (and therefore how many macro variables should be created) PROC SQL lets us omit the second part by simply coding the "-" with nothing behind it. 

 

In this case, the code is similar to

 

proc sql noprint;
   select v into : next1 - next7
   from have;
   %let n = &sqlobs.;
quit;
Tom
Super User Tom
Super User

What do you mean by "vector"?

What is you are planning to do with this list of values?

Why is that made easier instead of harder by creating all of those macro variables with similar names?

PaigeMiller
Diamond | Level 26

Great questions from @Tom . I look forward to hearing the answers from @Ronein .

 

In addition I would add that if these are months (2101 through 2107) then they should be handled as months instead of character strings. There are many benefits to handling months as months, for example if you ever need then next month after 2112, then it is 2201 rather than 2113, SAS makes this pretty easy, they have done the programming to handle months so you don't have to; but if you handle these as text strings you have to write your own logic.

 

How does one start a problem with a macro variable that has a value of

 

2101+2102+2103+2104+2105+2106+2107

 

?

 

It seems to me that lately you are starting with unusual macro variables, which are the cause of your problem, and if you had taken a different path, the whole problem could be bypassed. In my mind, macro variables come from calculations (for example, what month is six months before today? what is the mean value of X? does file ABC exist?), or by extracting data from databases or datasets. I can't imagine a problem where you start with a macro variable that has the value of

 

2101+2102+2103+2104+2105+2106+2107

 

and so my advice to you @Ronein is that you are working hard to get somewhere, and then you get stuck, and it would be easier to not go there in the first place, and we can help you with this, if only we knew what the big picture was (which you haven't told us). This is called the X-Y problem and is not uncommon, and it seems to me that this is often the real problem in your questions.

--
Paige Miller
ballardw
Super User

@PaigeMiller wrote:

Great questions from @Tom . I look forward to hearing the answers from @Ronein .

 

In addition I would add that if these are months (2101 through 2107) then they should be handled as months instead of character strings. There are many benefits to handling months as months, for example if you ever need then next month after 2112, then it is 2201 rather than 2113, SAS makes this pretty easy, they have done the programming to handle months so you don't have to; but if you handle these as text strings you have to write your own logic.

 

 

 I believe I may have pointed this out the OP more than 2 years ago. Maybe even more than once. Continued to do everything with that YYMM format. I also remember a set of questions related to dealing with intervals or similar uses of the values where repeatedly the first step was to create data values so the calculations could be done.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1065 views
  • 3 likes
  • 5 in conversation