BookmarkSubscribeRSS Feed
subrat1
Fluorite | Level 6

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

5 REPLIES 5
Cynthia_sas
SAS Super FREQ

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

subrat1
Fluorite | Level 6
I just wanted to separate columns name inside IF function
Reeza
Super User

@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.

Tom
Super User Tom
Super User

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

 

PGStats
Opal | Level 21

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 1001 views
  • 0 likes
  • 5 in conversation