DATA Step, Macro, Functions and more

Create distinct column from excel IF function

Reply
Contributor
Posts: 58

Create distinct column from excel IF function

DED_LAYER_BENCHMARK_LP = =if(_DataCell_ULFBL<=_InputCell_Deductible,

_DataCell_Original_Bench_LP_r0000_c0000,0) + if(_DataCell_ULFBL + _InputCell_Layer_r0000_c0000 <= _InputCell_Deductible, _DataCell_Original_Bench_LP_r0001_c0000, 0)

I want to create a new column which will have all distinct column used inside Excel IF function:
  _DataCell_ULFBL

_InputCell_Deductible

_DataCell_Original_Bench_LP_r0000_c0000

_DataCell_Original_Bench_LP_r0001_c0000

SAS Super FREQ
Posts: 9,252

Re: Create distinct column from excel IF function

[ Edited ]

Hi:
It's not clear to me what you want. If you already have an Excel sheet with a formula, SAS will NOT read or execute the formula.

If you want to create a new column in a SAS dataset based on another column in the dataset, then that is possible. Something like this:
data new;
set sashelp.class;
newage = age + 5;
if sex = 'F' then newht = height+.5;
else if sex = 'M' then newht = height+.75;
run;

In this DATA step program, every student in SASHELP.CLASS will have a new column created called NEWAGE by adding 5 to the AGE value in the data.

Then, every student will have a column called NEWHT that is assigned a value based on the value of the SEX variable.

Hope that helps,

cynthia

Contributor
Posts: 58

Re: Create distinct column from excel IF function

Posted in reply to Cynthia_sas
I just wanted to separate columns name inside IF function
Super User
Posts: 22,818

Re: Create distinct column from excel IF function


subrat1 wrote:
I just wanted to separate columns name inside IF function

It is absolutely not clear what you want. 

Instead of posting Excel, perhaps consider posting raw data and what you expect as output.

Super User
Super User
Posts: 7,844

Re: Create distinct column from excel IF function

[ Edited ]

Not sure what Excel has to do with the problem.  If you have a string that looks like this:

if(_DataCell_ULFBL<=_InputCell_Deductible
  ,_DataCell_Original_Bench_LP_r0000_c0000
  ,0
)
 + 
if(_DataCell_ULFBL + _InputCell_Layer_r0000_c0000 <= _InputCell_Deductible
  , _DataCell_Original_Bench_LP_r0001_c0000
  , 0
)

And you want to scan it to find distinct words then COUNTW() and SCAN() are useful.

Let's make some test data from your example.

data have ;
  str = '
if(_DataCell_ULFBL<=_InputCell_Deductible
  ,_DataCell_Original_Bench_LP_r0000_c0000
  ,0
)
 + 
if(_DataCell_ULFBL + _InputCell_Layer_r0000_c0000 <= _InputCell_Deductible
  , _DataCell_Original_Bench_LP_r0001_c0000
  , 0
)
' ;
run;

Now let's parse it into words.

data words ;
  set have ;
  length word $100 ;
  do i=1 to countw(str,'(, +-*/<=>)');
    word=upcase(scan(str,i,'(, +-*/)<=>'));
    output;
  end;
run;
proc sort nodupkey ;
  by word;
run;

You will probably want to figure out how to eliminate the function names and numeric constants.  And also the names that are longer than 32 characters and so invalid as SAS variable names.

image.png

 

Esteemed Advisor
Posts: 5,390

Re: Create distinct column from excel IF function

If you have access to an Excel formula as a text string then you could use pattern matching to extract variable names. Start with:

 

data test;
if not prxId then prxId + prxParse("/[[:alpha:]]\w*/o");
str = "DED_LAYER_BENCHMARK_LP = =if(_DataCell_ULFBL<=_InputCell_Deductible,
_DataCell_Original_Bench_LP_r0000_c0000,0) + if(_DataCell_ULFBL + 
_InputCell_Layer_r0000_c0000 <= _InputCell_Deductible, 
_DataCell_Original_Bench_LP_r0001_c0000, 0)";

start = 1;
stop = length(str);
call prxnext(prxId ,start, stop, str, pos, len);
do while (pos > 0);
    var = substr(str, pos, len);
    output;
    call prxnext(prxId, start, stop, str, pos, len);
    end;
keep var;
run;

proc print; run;
PG
Ask a Question
Discussion stats
  • 5 replies
  • 196 views
  • 0 likes
  • 5 in conversation