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

Used current SAS studio to execute the following program.

Length statement was applied to set the length (17) to "Smoking_Status" in the "if statement" below

However, still, the values for "Smoking_Status" are truncated (see below in the table attributes).

I would appreciate it if you could help me figure this out. 

Thanks! 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You missed one misspelling. 

If you are still seeing "truncation" then perhaps SMOKING_STATUS was already in the source dataset, HEART, and for someone accidentally attached a format to the variable that displays fewer than all 17 bytes of the value.

 

You could try removing any format that might have been attached to the variable.

data heart1; 
  length Smoking_Status $17;
  set heart;
  where status = "Alive" and not missing(AgeCHDdiag);
  format Smoking_Status ;
  if smoking < 0 then Smoking_Status = "Error";
  else if smoking <= 5 then Smoking_Status= "None (0–5)";
  else If smoking <= 15 then Smoking_Status = "Moderate (6–15)";
  else If smoking <= 25 then Smoking_Status = "Heavy (16–25)";
  else  Smoking_Status = "Very Heavy (>25)";
run;

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

LENGTH statements cannot be part of an IF statement. LENGTH statements cannot be executed conditionally. It applies to all observations of a variable.

 

We can't see your code, you did not include it in your message. (Do not attach files, include your code in your reply as text) If we could see your code, we could suggest other ways to do this.

--
Paige Miller
Naive_help
Calcite | Level 5
Here is the code -

data heart1 (drop=AgeatDeath Deathcause);
length Smoking_Status $ 17;
set heart;
where status = "Alive";
if AgeCHDdiag=. then delete;
If 0 =< smoking <= 6 then Smoking_Statuser = "None (0–5)";
else If 6 =< smoking <= 15 then Smoking_Statuser = "Moderate (6–15)";
else If 16 =< smoking <= 25 then Smoking_Statuser = "Heavy (16–25)";
else If smoking gt 25 then Smoking_Statuser = "Very Heavy (>25)";
else Smoking_Statuser = "Error";
run;
Tom
Super User Tom
Super User

The code is using a different variable name in the LENGTH statement than the one used in the series of IF/THEN/ELSE statements.

Naive_help
Calcite | Level 5
Thank you!
Made changes to code. I still see that length statement is not applied.

data heart1 (drop=AgeatDeath Deathcause);
length Smoking_Status $ 17;
set heart;
where status = "Alive";
if AgeCHDdiag=. then delete;
If 0 =< smoking <= 6 then Smoking_Status= "None (0–5)";
else If 6 =< smoking <= 15 then Smoking_Status = "Moderate (6–15)";
else If 16 =< smoking <= 25 then Smoking_Status = "Heavy (16–25)";
else If smoking gt 25 then Smoking_Status = "Very Heavy (>25)";
else Smoking_Statuser = "Error";
run;
ghosh
Barite | Level 11

You have overlapping ranges.  BTW you don't need to create the Smoking_Status variable.  Just use Proc format to display the label.  In case you do want to create the Smoking_Status variable use the put function 

 

Proc format;
  value statfmt (multilabel) 
	0–5= "None (0–5)"
    6–15 = "Moderate (6–15)"
    16–25 = "Heavy (16–25)"
	26-high	= "Very Heavy (>25)"
	other = "Error";
run;
data heart1 (drop=AgeatDeath Deathcause);
	length Smoking_Status $ 17;
	set heart;
	where status = "Alive";
	if AgeCHDdiag is missing then delete;
    format smoking statfmt.;
/* optional: to create the Smoking_Status var */
    Smoking_Status=put(Smoking_Status,statfmt.);
run;
Naive_help
Calcite | Level 5
Thank you! I was looking for help to identify the issue with the length statement in my code.
ghosh
Barite | Level 11
Well, you should show them log as well as proc content listing off your dataset.

You haven't provided any details for us to give you any meaningful answer
Tom
Super User Tom
Super User

You missed one misspelling. 

If you are still seeing "truncation" then perhaps SMOKING_STATUS was already in the source dataset, HEART, and for someone accidentally attached a format to the variable that displays fewer than all 17 bytes of the value.

 

You could try removing any format that might have been attached to the variable.

data heart1; 
  length Smoking_Status $17;
  set heart;
  where status = "Alive" and not missing(AgeCHDdiag);
  format Smoking_Status ;
  if smoking < 0 then Smoking_Status = "Error";
  else if smoking <= 5 then Smoking_Status= "None (0–5)";
  else If smoking <= 15 then Smoking_Status = "Moderate (6–15)";
  else If smoking <= 25 then Smoking_Status = "Heavy (16–25)";
  else  Smoking_Status = "Very Heavy (>25)";
run;
Naive_help
Calcite | Level 5
Excellent! Very insightful. Yes! Someone already had attached the format.
PaigeMiller
Diamond | Level 26

Hello @Naive_help 

Use custom formats instead of character strings and this avoids the entire LENGTH issue.

 

proc format;
    value smokef
        low-<0="Error"
        0-5="None (0-5)"
        6-15="Moderate (6-15)"
        16-25="Heavy (16-25)"
        26-high="Very Heavy (>25)";
run;
data heart1;  /* Or better yet, use PROC DATASETS to change a format */
    set heart;
    format smoking smokef.;
run;

 

 

Custom formats have another benefit ... the variable smoking remains numeric, and can be sorted NUMERICALLY. Your character variable  SMOKING_STATUS will sort alphabetically, this is not what you want, and so any reports or tables created with a SMOKING_STATUS will look out-of-order.

--
Paige Miller

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
  • 10 replies
  • 948 views
  • 1 like
  • 4 in conversation