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

 

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Phil_NZ
Barite | Level 11

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:

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lefunctionsref&docsetTarg...

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lefunctionsref&docsetTarg...

 

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.

View solution in original post

8 REPLIES 8
Astounding
PROC Star

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.

ballardw
Super User

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.

 

JibJam221
Obsidian | Level 7
thank you for this! Question - do you know if there is a way to do this for character variables as well?
Phil_NZ
Barite | Level 11

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:

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lefunctionsref&docsetTarg...

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lefunctionsref&docsetTarg...

 

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
ballardw
Super User

@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
Obsidian | Level 7
it works! Thank you x a million!
andreas_lds
Jade | Level 19

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

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 8 replies
  • 6625 views
  • 3 likes
  • 5 in conversation