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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

14 REPLIES 14
PaigeMiller
Diamond | Level 26

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).

--
Paige Miller
Armand_B
Calcite | Level 5

will you explain with code i would appreciate

ghosh
Barite | Level 11

You dont need to but if you must create the char variable, replace the format statememt with

Smoking_Status=(put (smoking,smoking.));
Armand_B
Calcite | Level 5

Sorry i dont understand your suggestion

I add it at the proc freq and at  data  i end up with 2 errors

ghosh
Barite | Level 11
please show your code and log
Armand_B
Calcite | Level 5
 
68
69 proc format;
70 value smoking
71 0 -<6 ="None(0-5)"
72 6- 15 ="Moderate(6-15)"
73 16-25 ="Heavy(16-25)"
74 26 - high= "Very Heavy(>25)"
75 other ="Error";
NOTE: Format SMOKING has been output.
76 run;
 
NOTE: PROCEDURE FORMAT a utilisé (Durée totale du traitement) :
real time 0.00 seconds
user cpu time 0.01 seconds
system cpu time 0.01 seconds
memory 302.68k
OS Memory 23968.00k
Timestamp 05/01/2022 08:58:12 PM
Step Count 61 Switch Count 0
Page Faults 0
Page Reclaims 41
Page Swaps 0
Voluntary Context Switches 0
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 40
 
 
77
78 libname cert xlsx "/home/u49144401/Cert/heart.xlsx";
NOTE: Libref CERT was successfully assigned as follows:
Engine: XLSX
Physical Name: /home/u49144401/Cert/heart.xlsx
79 data work.hearts;
80 set cert.heart(drop=AgeAtDeath DeathCause);
81 where Status='Alive';
82 if AgeCHDiag=. then delete;
83 length Smoking_Status $17;
WARNING: Length of character variable Smoking_Status has already been set.
Use the LENGTH statement as the very first statement in the DATA STEP to declare the length of a character variable.
84 Smoking_Status=(put (smoking,smoking.));
85 run;
 
NOTE: Variable AgeCHDiag is uninitialized.
NOTE: The import data set has 5209 observations and 17 variables.
NOTE: There were 3218 observations read from the data set CERT.heart.
WHERE Status='Alive';
NOTE: The data set WORK.HEARTS has 0 observations and 16 variables.
NOTE: DATA statement a utilisé (Durée totale du traitement) :
real time 0.74 seconds
user cpu time 0.74 seconds
system cpu time 0.00 seconds
memory 2971.40k
OS Memory 26532.00k
Timestamp 05/01/2022 08:58:13 PM
Step Count 62 Switch Count 2
Page Faults 0
Page Reclaims 685
Page Swaps 0
Voluntary Context Switches 18
Involuntary Context Switches 1
Block Input Operations 0
Block Output Operations 264
 
 
86
87 proc freq data=work.hearts;
88 tables AgeCHDiag*Smoking_Status/norow nocol nopercent;
89 Smoking_Status=(put (smoking,smoking.));
______________
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
90 run;
 
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE FREQ a utilisé (Durée totale du traitement) :
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 445.53k
OS Memory 24740.00k
Timestamp 05/01/2022 08:58:13 PM
Step Count 63 Switch Count 0
Page Faults 0
Page Reclaims 50
Page Swaps 0
Voluntary Context Switches 0
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 0
 
 
 
ghosh
Barite | Level 11
Smoking_Status=(put (smoking,smoking.));
Only in the data step and not in the proc. I'm assuming you are creating
this var to include in your excel file, otheri you don't it
Armand_B
Calcite | Level 5

I am ending up with 2 errors

data work.hearts;
79 set work.heart(drop=AgeAtDeath DeathCause);
ERROR: The variable AgeAtDeath in the DROP, KEEP, or RENAME list has never been referenced.
ERROR: The variable DeathCause in the DROP, KEEP, or RENAME list has never been referenced.
80 where Status='Alive';
81 if AgeCHDiag=. then delete;
82
83 length Smoking_Status $17;
84 smoking_Status=(put(smoking,smoking.));
85 run;
 
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.HEARTS may be incomplete. When this step was stopped there were 0 observations and 3 variables.
WARNING: Data set WORK.HEARTS was not replaced because the step was stopped
Reeza
Super User
That log is not from the code I posted. Post the log from the code I posted.
Reeza
Super User

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;

 

Armand_B
Calcite | Level 5

Thanks for your time it is well written and with comments  but.......we end up with the same issue

Log file message

NOTE: Variable AgeCHDiag is uninitialized.
NOTE: The import data set has 5209 observations and 17 variables.
NOTE: There were 3218 observations read from the data set CERT.heart.
WHERE Status='Alive';
NOTE: The data set WORK.HEARTS has 0 observations and 16 variables.

NOTE: No observations in data set WORK.HEARTS.

PS 

how comes  ? what went wrong ?

Reeza
Super User

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;
Reeza
Super User
It's worth noting both your errors were due to incorrectly referencing variable names - in one case referring to the wrong variable and in the other misspelling a variable name.
Armand_B
Calcite | Level 5

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

sas-innovate-2024.png

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.

 

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
  • 14 replies
  • 940 views
  • 0 likes
  • 4 in conversation