Was asked to make many character variables into numeric ones but cannot seem to run my code. The question is asking me to make a new variable called "HTC_STATn" which equals 1 is there new patients as the value and 0 is there is established patients.
When I run this code the dataset is blank
data temp2;
set demomed;
If htc_stat= "established patient" then HTC_STATn="0";
If htc_stat= "new patient" then HTC_STATn= "1";
run;
data temp2;
set demomed;
If htc_stat= "established patient" then HTC_STATn="0";
If htc_stat= "new patient" then HTC_STATn= "1";
If htc_stat= "transfer patient" then HTC_STATn= "1";
If hepther = "y" HEPTHERn= "1";
If hepther = "n" HEPTHERn= "0";
If Htype = "hemophilia a" then HTYPEn = "0";
If Htype = "hemophilia b" then HTYPEn = "1";
If Hlevel = 'mild' then Hleveln = "0";
If Hlevel = 'moderate' then Hleveln = "1";
If Hlevel = 'severe' then Hleveln = "2";
run;
The CtoN macro is a general tool for character to numeric conversion. Your specific code just creates a new character variable with character values "0" and "1". If you want it to be numeric, remove the quotes:
If htc_stat= "established patient" then HTC_STATn=0;
If htc_stat= "new patient" then HTC_STATn= 1;
Please post the complete (all code and messages) by copy/pasting the log text into a window opened with this button:
The question is asking me to make a new variable called "HTC_STATn" which equals 1 is there new patients as the value and 0 is there is established patients.
SAS return a 1 or 0 as the result of a boolean expression. So just use:
data temp2;
set demomed;
HTC_STATn = (htc_stat= "new patient");
run;
@saza wrote:
When I run this code the dataset is blank
Show us the results of running Proc Contents on the data set DEMOMED.
If the result data set is blank that strongly suggests that the source data set was as well.
a bit like :
HTC_STATn="0";
Does not assign a numeric value to HTC_STATn but a character value of "0". A numeric assignment would look like
HTC_STATn= 0 ;
Also, you want to be very careful about = with character variables. "Mild" is not equal to "mild" so make sure that case is considered.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: