- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The code is using a different variable name in the LENGTH statement than the one used in the series of IF/THEN/ELSE statements.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You haven't provided any details for us to give you any meaningful answer
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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