Your SAS programs, embedded in web apps and elsewhere

Let Compute a Value rather than Store a String

Accepted Solution Solved
Reply
Super Contributor
Posts: 477
Accepted Solution

Let Compute a Value rather than Store a String

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.


Accepted Solutions
Solution
‎05-14-2015 09:29 AM
Super User
Posts: 5,069

Re: Let Compute a Value rather than Store a String

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


All Replies
Super User
Posts: 5,069

Re: Let Compute a Value rather than Store a String

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.

Super Contributor
Posts: 275

Re: Let Compute a Value rather than Store a String

%let measureSummed = &measureTy || &_sum;

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

%put &measureSummed;

Super Contributor
Posts: 477

Re: Let Compute a Value rather than Store a String

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;

Super User
Super User
Posts: 7,392

Re: Let Compute a Value rather than Store a String

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.

Solution
‎05-14-2015 09:29 AM
Super User
Posts: 5,069

Re: Let Compute a Value rather than Store a String

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?

Super Contributor
Posts: 477

Re: Let Compute a Value rather than Store a String

This solved it:

%let measureSummed = &measureTy._sum;

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 441 views
  • 6 likes
  • 4 in conversation