I have received a data set with a date (bene_death_dt) coded as an 8 digit character string (according to proc contents).
When I check values of the variable with proc freq, it show that missing data is noted with a period.
No matter how I write my code, I cannot reference those values of "." and do anything with them.
For example, I want to code:
if bene_death_dt = "." then died = 0;
else died = 1;
The result is that all values of bene_death_dt are assigned to died=1, including those that are "."
I get no errors in my log.
I'm stumped and would appreciate any pointers on where to check for issues.
if strip(trim(bene_death_dt)) = "." then died = 0;
else died = 1;
Try removing any extra spaces using TRIM/STRIP()?
if strip(trim(bene_death_dt)) = "." then died = 0;
else died = 1;
Try removing any extra spaces using TRIM/STRIP()?
Including TRIM() is not adding anything.
TRIM() removes trailing spaces (which SAS ignores anyway).
STRIP() remove leading and trailing spaces.
LEFT() removes leading spaces, which all you really need.
One way to create a character variable with a period and leading spaces is to use the PUT() function with a missing value. In that case the period will put in the character position of the WIDTH of the format used. So code like:
string=put(date,date9.);
Will generate a string with 8 spaces followed by a period.
Note such leading spaces are made even harder to see if you display your data use ODS output (which is now the default) since ODS "eats" the leading spaces for some unknown reason.
You can also avoid generating the period at all by changing the character used in the MISSING option to a space instead of the default period before running the step that used the PUT() function.
First I would double-check that PROC CONTENTS shows you that this variable is defined as CHARACTER, rather than NUMERIC. Sometimes people do stored dates in a CHARACTER variable, but it's not a good idea.
If it really is character, then I would think you may have a problem with leading blanks in your data. So you could try something simple like:
if left(bene_death_dt) = "." then died = 0;
else died = 1;
If that doesn't work then you might have other unprintable characters (e.g. tabs) in your character value you could try:
if compress(bene_death_dt,,'C') = "." then died = 0;
else died = 1;
If either of those work, you would want to go back an clean the data in bene_death_dt.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.