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.

 

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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