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

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