Hi everyone, hoping to get some help on this one.
I have a table that holds customerIDs. There are two other columns called `dateofpurchase` and 'dateofvisit'. When 'dateofpurchase' is empty, im hoping to full these cells with the same 'dateofvisit' values, kind of like a default. However, if the 'dateofpurchase' is not empty, I want to keep it as is.
I do not have code to attach since I don't know where to start.
Thanks!
I think you can do
data want (rename(dateofpurchase1=dateofpurchase) drop=dateofpurchase1 );
set have;
dateofpurchase1= ifn(' ',dateofvisit,dateofpurchase)
run;
And regarding the character value , you can change from "ifn" to "ifc" as @ballardw mentioned.
More documents for you:
Surely you could give us some sample data .... what it looks like before and what you want it to look like. Be sure to include one example where a CustomerID is missing its very first dateofpurchase.
One way would look something like this:
data want; set have; if missing(dateofpurchase) then dateofpurchase=dateofvisit; run;
The missing function tests a variable to see if it is missing and returns "true" (actually a numeric value of 1) when the variable does not have a value. If the variable is character consisting only of blanks it will also result in true.
I think you can do
data want (rename(dateofpurchase1=dateofpurchase) drop=dateofpurchase1 );
set have;
dateofpurchase1= ifn(' ',dateofvisit,dateofpurchase)
run;
And regarding the character value , you can change from "ifn" to "ifc" as @ballardw mentioned.
More documents for you:
@Phil_NZ wrote:
I think you can do
data want (rename(dateofpurchase1=dateofpurchase) drop=dateofpurchase1 ); set have; dateofpurchase1= ifn(' ',dateofvisit,dateofpurchase) run;
And I think it can be applied for both numeric and character types of var.
Actually IFN works with numeric values. Character values require IFC.
@JibJam221 wrote:
thank you for this! Question - do you know if there is a way to do this for character variables as well?
The code posted by @ballardw works idependent of the variables' type. Both variables used in the assignment must have, of course, the same type.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.