turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- resolving a formula in a dataset

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-01-2011 02:44 PM

Hi,

I have a table with 2 columns. One is simple formula and the other one a numeric variable. I am trying to resolve the formula in the dataset but it does not work and I am not sure to understand why. Below is the example:

DATA temp1;

INPUT formula $ var1 ;

DATALINES;

2*var1 1

23*var1 2

12*var1 5

;

data temp2;

set temp1;

format result 8.;

result=formula;

run;

Why is it generating an error? if ifdo something like this, it is perfectly fine.

data temp2;

set temp1;

format result 8.;

result=var1*5;

run;

Thnaks a lot

I have a table with 2 columns. One is simple formula and the other one a numeric variable. I am trying to resolve the formula in the dataset but it does not work and I am not sure to understand why. Below is the example:

DATA temp1;

INPUT formula $ var1 ;

DATALINES;

2*var1 1

23*var1 2

12*var1 5

;

data temp2;

set temp1;

format result 8.;

result=formula;

run;

Why is it generating an error? if ifdo something like this, it is perfectly fine.

data temp2;

set temp1;

format result 8.;

result=var1*5;

run;

Thnaks a lot

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to mrom34

02-01-2011 03:04 PM

Your variable Formula is only a character variable.

This means for the first observation in your second dataset is equivalent to

result="2*var1";

which is not the same as

result=2*var1;

Only macro variables resolve the way that you are thinking. You should take another look at what you are trying to accomplish and try to figure out a simpler solution.

This means for the first observation in your second dataset is equivalent to

result="2*var1";

which is not the same as

result=2*var1;

Only macro variables resolve the way that you are thinking. You should take another look at what you are trying to accomplish and try to figure out a simpler solution.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to mrom34

02-02-2011 01:01 AM

Assuming that you will always multiply with Var1.

Strip the first value before the '*' in Formula and then use it to multiply with Var1.

DATA temp1;

INPUT formula $ var1 ;

DATALINES;

2*var1 1

23*var1 2

12*var1 5

;

run;

data temp2;

set temp1;

format result 8.;

*result=formula;

result=input(scan(formula,1,'*'),best.) * Var1;

run;

proc print data=temp2;

run;

Obs formula var1 result

1 2*var1 1 2

2 23*var1 2 46

3 12*var1 5 60

When you assign Formula to Result, you are merely copying over the value to Result.

I don't think you can resolve a formula in a datastep. Message was edited by: bhavani

Strip the first value before the '*' in Formula and then use it to multiply with Var1.

DATA temp1;

INPUT formula $ var1 ;

DATALINES;

2*var1 1

23*var1 2

12*var1 5

;

run;

data temp2;

set temp1;

format result 8.;

*result=formula;

result=input(scan(formula,1,'*'),best.) * Var1;

run;

proc print data=temp2;

run;

Obs formula var1 result

1 2*var1 1 2

2 23*var1 2 46

3 12*var1 5 60

When you assign Formula to Result, you are merely copying over the value to Result.

I don't think you can resolve a formula in a datastep. Message was edited by: bhavani

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to bhavani

02-02-2011 10:17 AM

See this posting from a couple of months ago. Chang showed a solution here that proves it is indeed possible to resolve a formula in a datastep.

http://support.sas.com/forums/thread.jspa?messageID=47101럽

http://support.sas.com/forums/thread.jspa?messageID=47101럽

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to mrom34

02-22-2011 11:44 PM

Hi.

If you have very large dataset :

[pre]

DATA temp1;

INPUT formula $ var1 ;

DATALINES;

2*var1 1

23*var1 2

12*var1 5

;

data result;

set temp1;

temp=tranwrd(formula ,'var1',strip(var1));

result=resolve('%sysevalf('||temp||')');

run;

[/pre]

If you have not very large dataset:

[pre]

DATA temp1;

INPUT formula $ var1 ;

DATALINES;

2*var1 1

23*var1 2

12*var1 5

;

data _null_;

set temp1 end=last;

if _n_=1 then call execute('data _result;');

call execute('var1='||var1||';');

call execute('formula="'||formula||'";');

call execute('result='||formula||';');

call execute('output;');

if last then call execute('run;');

run;

[/pre]

Ksharp

If you have very large dataset :

[pre]

DATA temp1;

INPUT formula $ var1 ;

DATALINES;

2*var1 1

23*var1 2

12*var1 5

;

data result;

set temp1;

temp=tranwrd(formula ,'var1',strip(var1));

result=resolve('%sysevalf('||temp||')');

run;

[/pre]

If you have not very large dataset:

[pre]

DATA temp1;

INPUT formula $ var1 ;

DATALINES;

2*var1 1

23*var1 2

12*var1 5

;

data _null_;

set temp1 end=last;

if _n_=1 then call execute('data _result;');

call execute('var1='||var1||';');

call execute('formula="'||formula||'";');

call execute('result='||formula||';');

call execute('output;');

if last then call execute('run;');

run;

[/pre]

Ksharp