BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DavidPhillips2
Rhodochrosite | Level 12

Is there a way to compute and store rather than just store a string to be resolved later?

%let measureSummed = &measureTy || _sum;

I would like measureSummed to store the value of &measureTy + _sum instead of

&measureTy || _sum.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

If measureTy is a macro variable, this is easy:

%let measureSummed = &measureTy._sum;

(It gets a little more complicated if &MeasureTy contains an embedded blank.)

But your description makes it sound like measureTy is a DATA step variable.  In that case, SAS can do what you want, but the process needs to be split into two steps.  The reason is that SAS needs to know all the variables that a DATA step will process before it runs the DATA step.  But running the DATA step is required to examine the value of measureTy.  So here is a possibility:

data _null_;

   set have;

   call symputx('measureSummed', strip(measureTy) || '_sum');

run;

Or if measureTy contains an embedded blank that needs to be converted to an underscore:

data _null_;

   set have;

   call symputx('measureSummed', translate(strip(measureTy), '_', ' ') || '_sum');

run;

Then later in the program you can refer to &measureSummed.

Are we getting close?

View solution in original post

6 REPLIES 6
Astounding
PROC Star

In macro language, _sum is just 4 characters.  It is not something that can be added.  There are other variations that you might be looking for, such as:

%let measureSummed = &measureTy._sum;

%let measureSummed = %eval(&measureTy + &_sum);

It's possible you can do what you want to, but it's not 100% clear what you are trying to get from this.

Good luck.

slchen
Lapis Lazuli | Level 10

%let measureSummed = &measureTy || &_sum;

%let measureSummed=%sysevalf(%sysfunc(translate(&measureSummed,+,||)));

%put &measureSummed;

DavidPhillips2
Rhodochrosite | Level 12

I’m researching a few of the keywords used in your example.

To clarify I have a column that is dynamically generated and SAS appends the text _sum to it.  measureTy is the first half of the column name. _sum is the second half.  Note _sum is not a variable.  An example of the full created column name is students_enrolled_sum.  Where measureTy is students enrolled and _sum needs to be appended.


When I use:

data enrollment; set enrollment;

rename &measureSummed = n;

run;

I can’t have two strings in the rename &measureSummed = n;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

There is the sashelp metadata files which contain the information about all tables/columns.  I find using that is easier than fiddling around with lists of ampersands, quoting etc.:

data _null_;

     set sashelp.vcolumn (where=(libname="WORK" and memname="XYZ" and substr(strip(reverse(NAME)),1,3)="MUS"));

     call symputx('YOUR_VAR',name);

run;

Or just generate the necessary code from a call execute.  Or alternatively keep a running dataset with the variable name plus _sum, and use that.

Astounding
PROC Star

If measureTy is a macro variable, this is easy:

%let measureSummed = &measureTy._sum;

(It gets a little more complicated if &MeasureTy contains an embedded blank.)

But your description makes it sound like measureTy is a DATA step variable.  In that case, SAS can do what you want, but the process needs to be split into two steps.  The reason is that SAS needs to know all the variables that a DATA step will process before it runs the DATA step.  But running the DATA step is required to examine the value of measureTy.  So here is a possibility:

data _null_;

   set have;

   call symputx('measureSummed', strip(measureTy) || '_sum');

run;

Or if measureTy contains an embedded blank that needs to be converted to an underscore:

data _null_;

   set have;

   call symputx('measureSummed', translate(strip(measureTy), '_', ' ') || '_sum');

run;

Then later in the program you can refer to &measureSummed.

Are we getting close?

DavidPhillips2
Rhodochrosite | Level 12

This solved it:

%let measureSummed = &measureTy._sum;

measureTy is a marco variable that I concatenated with the text _sum in the datastep.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1089 views
  • 6 likes
  • 4 in conversation