Desktop productivity for business analysts and programmers

Something with IF/THEN. Yet, I'm a SAS dummy.

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Something with IF/THEN. Yet, I'm a SAS dummy.

Hi all,

 

I'm new here, as well as I'm quite new on SAS Enterprise Guide. I have a question about what I think should be an IF/THEN-statement, but since I'm still a noob, I don't know how to perform this statement correctly.

 

I have a column (let's call it COLUMNONE) with thousands of codes and a second column with codes (COLUMNTWO). What I would like to see is that if COLUMNONE contains a certain value, then that value should be replaced with the value stated in COLUMNTWO. If COLUMNONE does not contain that specific value (but something else), then nothing should happen.

 

Example.

COLUMNONE contains codes like 5.19.XXXXXXX or 6.07.XXXXXXX, where X are random numbers. Now, if COLUMNONE has a code that starts with '5.19.' (what comes on the Xs is not relevant), then I want the code that is stated in COLUMNTWO. If COLUMNONE has a code other then starting with '5.19.' it's fine and no changes are needed.

 

Can anyone please provide the steps I need to take in order to perform this?? If you need any more information please ask!

 

Cheers 


Accepted Solutions
Solution
‎02-19-2016 09:01 AM
Community Manager
Posts: 2,889

Re: Something with IF/THEN. Yet, I'm a SAS dummy.

Try to add an advanced expression in the Expression editor  like this:

 

case 
 when substr(t1.columnone,1,4)= '5,19' then t1.columntwo
 else t1.columnone
end

The query builder will add the "AS newcolumnname" at the end to create your new calculated column

View solution in original post


All Replies
Super User
Posts: 5,388

Re: Something with IF/THEN. Yet, I'm a SAS dummy.

If get you logic correctly:


If you are using the query builder in EG, you need to think SQL case-when-then-else instead. For this, there is a kind of point-and-click interface.

 

Using progamming with the data step (untested):

 

 

data want;
    set have;
    if substr(columnone,1,4) = '5,19' then columnone = cats(substr(columntwo,1,4),substr(columnsone,5));
run;

 

Data never sleeps
New Contributor
Posts: 3

Re: Something with IF/THEN. Yet, I'm a SAS dummy.

Thanks for the quick reply.

 

Is it also possible to build this through the computed column editor (advanced expression)?

Solution
‎02-19-2016 09:01 AM
Community Manager
Posts: 2,889

Re: Something with IF/THEN. Yet, I'm a SAS dummy.

Try to add an advanced expression in the Expression editor  like this:

 

case 
 when substr(t1.columnone,1,4)= '5,19' then t1.columntwo
 else t1.columnone
end

The query builder will add the "AS newcolumnname" at the end to create your new calculated column

New Contributor
Posts: 3

Re: Something with IF/THEN. Yet, I'm a SAS dummy.

Great. Thanks. It works!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 536 views
  • 1 like
  • 3 in conversation