Hello
i m trying to rewrite a solution( which i found not aesthetic) here is the solution
libname cert xlsx "/home/u49144401/Cert/heart.xlsx";
data work.heart;
set cert.heart(drop=AgeAtDeath DeathCause);
where Status='Alive';
if AgeCHDiag=. then delete;
length Smoking_Status $17;
if 0<=Smoking<6 then Smoking_Status='Non-smoker(0-5)';
if 6<=Smoking<=15 then Smoking_Status='Moderate(6-15)';
if 16<=Smoking<25 then Smoking_Status='Heavy(16-25)';
if Smoking>25 then Smoking_Status='Very Heavy(16-25)';
else Smoking_Status='Error';
run;
proc freq data=work.heart;
tables AgeCHDiag*Smoking_status/norow nocol nopercent;
run;
Following is the code i m trying to write...But i have spent more than 2 hours to try to understand what is wrong...please help
Regards
proc format;
value smoking
0 -<6 ="None(0-5)"
6- 15 ="Moderate(6-15)"
16-25 ="Heavy(16-25)"
26 - high= "Very Heavy(>25)"
other ="Error";
run;
libname cert xlsx "/home/u49144401/Cert/heart.xlsx";
data work.hearts;
set cert.heart(drop=AgeAtDeath DeathCause);
where Status='Alive';
if AgeCHDiag=. then delete;
length Smoking_Status $17;
format Smoking_Status smoking.;
run;
proc freq data=work.hearts;
tables AgeCHDiag*Smoking_Status/norow nocol nopercent;
format Smoking_Status smoking.;
run;
LOGFILE ERROR: You are trying to use the numéric format SMOKING with the character variable Smoking_Status in data set WORK.HEARTS
The variable is ageCHDDiag - you were missing a D in your code and without checking the actual data set we couldn't know that.
proc format;
value smoking 0 -<6="None(0-5)" 6- 15="Moderate(6-15)" 16-25="Heavy(16-25)"
26 - high="Very Heavy(>25)" other="Error";
run;
*creates a library to read in data;
libname cert xlsx "/home/u49144401/Cert/heart.xlsx";
*creates a data set called hearts;
data hearts;
*reads in heart data set;
set sashelp.heart(drop=AgeAtDeath DeathCause);
*keeps only records where people are alive;
where Status='Alive';
*deletes records where missing Age of diagnosis - could be included in the WHERE clause for more efficient code;
if AgeCHDdiag=. then
delete;
*Your variable is smoking - not smoking status;
*applies format smoking to your smoking variable;
format Smoking smoking.;
*creates smoking_status variable as a character variable;
*this converts the variable. The format is also applied so this step is optional;
Smoking_Status=put(smoking, smoking.);
run;
title 'Smoking Variable - with MISSING';
proc freq data=hearts;
tables AgeCHDDiag*Smoking / norow nocol nopercent missing;
run;
title 'Smoking Status Variable - with ERROR';
proc freq data=hearts;
tables AgeCHDDiag*Smoking_STATUS / norow nocol nopercent;
run;
Smoking_status is a character variable. You would have to use a character format on it, but the format is a numeric format. You can't apply numeric formats to character variables.
What you really want to do is to apply format SMOKING to variable SMOKING (and get rid of variable SMOKING_STATUS).
will you explain with code i would appreciate
You dont need to but if you must create the char variable, replace the format statememt with
Smoking_Status=(put (smoking,smoking.));
Sorry i dont understand your suggestion
I add it at the proc freq and at data i end up with 2 errors
I am ending up with 2 errors
Commenting and formatting your code would really really help you here.
You can use SAS autoformat options at the least to help make your code legible.
*creates a format for a numeric variable;
proc format;
value smoking 0 -<6="None(0-5)" 6- 15="Moderate(6-15)" 16-25="Heavy(16-25)"
26 - high="Very Heavy(>25)" other="Error";
run;
*creates a library to read in data;
libname cert xlsx "/home/u49144401/Cert/heart.xlsx";
*creates a data set called hearts;
data hearts;
*reads in heart data set;
set cert.heart(drop=AgeAtDeath DeathCause);
*keeps only records where people are alive;
where Status='Alive';
*deletes records where missing Age of diagnosis - could be included in the WHERE clause for more efficient code;
if AgeCHDiag=. then
delete;
*Your variable is smoking - not smoking status;
*applies format smoking to your smoking variable;
format Smoking smoking.;
*creates smoking_status variable as a character variable;
*this converts the variable. The format is also applied so this step is optional;
Smoking_Status=put(smoking, smoking.);
run;
title 'Smoking Variable';
proc freq data=hearts;
tables AgeCHDiag*Smoking / norow nocol nopercent;
run;
title 'Smoking Status Variable';
proc freq data=hearts;
tables AgeCHDiag*Smoking_STATUS / norow nocol nopercent;
run;
Thanks for your time it is well written and with comments but.......we end up with the same issue
Log file message
NOTE: No observations in data set WORK.HEARTS.
PS
how comes ? what went wrong ?
The variable is ageCHDDiag - you were missing a D in your code and without checking the actual data set we couldn't know that.
proc format;
value smoking 0 -<6="None(0-5)" 6- 15="Moderate(6-15)" 16-25="Heavy(16-25)"
26 - high="Very Heavy(>25)" other="Error";
run;
*creates a library to read in data;
libname cert xlsx "/home/u49144401/Cert/heart.xlsx";
*creates a data set called hearts;
data hearts;
*reads in heart data set;
set sashelp.heart(drop=AgeAtDeath DeathCause);
*keeps only records where people are alive;
where Status='Alive';
*deletes records where missing Age of diagnosis - could be included in the WHERE clause for more efficient code;
if AgeCHDdiag=. then
delete;
*Your variable is smoking - not smoking status;
*applies format smoking to your smoking variable;
format Smoking smoking.;
*creates smoking_status variable as a character variable;
*this converts the variable. The format is also applied so this step is optional;
Smoking_Status=put(smoking, smoking.);
run;
title 'Smoking Variable - with MISSING';
proc freq data=hearts;
tables AgeCHDDiag*Smoking / norow nocol nopercent missing;
run;
title 'Smoking Status Variable - with ERROR';
proc freq data=hearts;
tables AgeCHDDiag*Smoking_STATUS / norow nocol nopercent;
run;
Thanks for your comittment...Now the solution work very well
a) i had the SAS Prep Guide it has typing errors.....so i forgot to list the dataset in order to see whether names were properly written
b) i spent 3 hours thinking about rewriting the solution....with your help i was successful
Thanks for saving me this worry
my regards
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.