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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 8 replies
  • 6450 views
  • 3 likes
  • 5 in conversation