BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Margot89
Calcite | Level 5

Hi Everyone,

 

Currently I am working in SAS Enterprise Guide. I'm trying to create a new column from two columns using advanced expression. The idea is this: There is a column named 'Department' and one named 'Project'. If in 'Project' the characters contain 'XYZ', then the value in 'Department' needs to be adjusted to 'ABC'. If, in 'Project' the characters do not contain 'XYZ', we need to keep using the value already present in 'Department'. So basically I'm trying to replace a value in 'Department', based on the values in 'Project'. I'm trying to use FIND within a IF ELSE statement, but so far it's not working. Searches online are not really helping. 

 

Does anyone have a suggestion as to how I can achieve this result? Many thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
sustagens
Pyrite | Level 9

After selecting Advanced Expression click Next then type this into the Enter an Expression box

case when find(project,'XYZ') ne 0 then 'ABC' else Department end

View solution in original post

4 REPLIES 4
mklangley
Lapis Lazuli | Level 10

Not having the data you're working with, I made some up. Give this a try:

data have;
    input department $ project & $15.;
    datalines;
    dept_1  QWERTY
    dept_2  WXYZ
    dept_3  123-XYZ
    dept_4  not capital xyz
    ;
run;

data want;
    set have;
    if find(project, 'XYZ') ge 1
        then department = 'ABC';
run;
Kurt_Bremser
Super User

See this data step code:

data have;
input project :$3. department :$3.;
datalines;
PRQ AAA
XYZ BBB
;

data want;
set have;
if find(project,'XYZ') then department = 'ABC';
run;

With SQL, it looks like this:

proc sql;
create table want as
  select
    project,
    case
      when find(project,'XYZ') then 'ABC'
      else department
    end as department
  from have
;
quit;

If you need to create an expression in EG's Query Builder, you need to use SQL syntax (case-when-then-else-end)

sustagens
Pyrite | Level 9

After selecting Advanced Expression click Next then type this into the Enter an Expression box

case when find(project,'XYZ') ne 0 then 'ABC' else Department end
Margot89
Calcite | Level 5

Thanks, this turned out to be the proper solution!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1905 views
  • 1 like
  • 4 in conversation