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

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. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
if strip(trim(bene_death_dt)) = "." then died = 0;
else died = 1;

Try removing any extra spaces using TRIM/STRIP()?

View solution in original post

4 REPLIES 4
Reeza
Super User
if strip(trim(bene_death_dt)) = "." then died = 0;
else died = 1;

Try removing any extra spaces using TRIM/STRIP()?

greesamu
Obsidian | Level 7
Huzzah, thank you so much!
Tom
Super User Tom
Super User

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.

 

Quentin
Super User

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.

 

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

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
  • 4 replies
  • 1084 views
  • 3 likes
  • 4 in conversation