I have a data set like this.
data RISKCLNS.RisksOnly_Apr17_Apr19(keep=REASON INSURANCE VISIT_DT PATIENT_ID _01_SEX_W_MALE _08_SEX_W_MAN_WH_SEX_W_MAN _02_SEX_W_FEMALE A__3_PARTNERS_IN_90_DAYS
ALCOHOL_USE ANONYMOUS_SEX DRUG_USE DRUG_USE_MARIJUANA SEX_W_DRG_ETOH__NEVER_ SEX_W_NON_INJ_DRUG_USER SEX_W_DRG_ETOH_MOST_TIMES_ SEX_W_DRG_ETOH_SOMETIMES SEX_W_DRG_ETOH_MOST_TIMES
SEX_W_DRG_ETOH__ALWAYS_ _06D_HAD_AN_STD_IN_PAST_3Y _06B_HAD_GC_IN_PAST_3_YRS A_NO_PARTNERS_IN_90_DAYS A__2_PARTNERS_IN_90_DAYS NEW_PARTNER_LAST_VISIT SEX_W_PRTNR_MET_INTERNET
A__1_PARTNER_IN_90_DAYS _04_SEX_WHILE_USING_NON_ID A__4_9_PARTNRS_IN_90_DAYS RECTAL_INTERCOURSE ECSTACY__METHAMPHETAMINES
_10_SEX_PARTNERS_90_DAYS DRUG_USE_METHAMPHETAMINES SEX_W_TRANSGENDER _07_SEX_W_IDU COCAINE_USE_SNORT _03_USING_USED_INJ_DRUGS
DRUG_USE_HEROIN COCAINE_USE_INJECT DRUG_USE_OPIOIDS SEX_FOR_DRUGS_MONEY GENDER_SP);
set RISKCLNS.KNNRSK_Apr17_Apr19 ;
run;
I want to convert all numeric variables into character. There are two character variables and rest of
variables are numeric. I used this code below, it did not work, Variables are still numeric,
data want;
set RISKCLNS.RisksOnly_Apr17_Apr19;
array charn(41) PATIENT_ID _01_SEX_W_MALE _08_SEX_W_MAN_WH_SEX_W_MAN _02_SEX_W_FEMALE A__3_PARTNERS_IN_90_DAYS
ALCOHOL_USE ANONYMOUS_SEX DRUG_USE DRUG_USE_MARIJUANA SEX_W_DRG_ETOH__NEVER_ SEX_W_NON_INJ_DRUG_USER SEX_W_DRG_ETOH_MOST_TIMES_ SEX_W_DRG_ETOH_SOMETIMES SEX_W_DRG_ETOH_MOST_TIMES
SEX_W_DRG_ETOH__ALWAYS_ _06D_HAD_AN_STD_IN_PAST_3Y _06B_HAD_GC_IN_PAST_3_YRS A_NO_PARTNERS_IN_90_DAYS A__2_PARTNERS_IN_90_DAYS NEW_PARTNER_LAST_VISIT SEX_W_PRTNR_MET_INTERNET
A__1_PARTNER_IN_90_DAYS _04_SEX_WHILE_USING_NON_ID A__4_9_PARTNRS_IN_90_DAYS RECTAL_INTERCOURSE ECSTACY__METHAMPHETAMINES
_10_SEX_PARTNERS_90_DAYS DRUG_USE_METHAMPHETAMINES SEX_W_TRANSGENDER _07_SEX_W_IDU COCAINE_USE_SNORT _03_USING_USED_INJ_DRUGS
DRUG_USE_HEROIN COCAINE_USE_INJECT DRUG_USE_OPIOIDS SEX_FOR_DRUGS_MONEY GENDER_SP;
array charc(41) $12. PATIENT_ID1 - GENDER_SP41;
do i = 1 to 41;
charc(i)=put(charn(i),z11.);
end;
run;
The log says this:
156 data want;
157 set RISKCLNS.RisksOnly_Apr17_Apr19;
158 array charn(41) PATIENT_ID _01_SEX_W_MALE _08_SEX_W_MAN_WH_SEX_W_MAN _02_SEX_W_FEMALE
158! A__3_PARTNERS_IN_90_DAYS
159 ALCOHOL_USE ANONYMOUS_SEX DRUG_USE DRUG_USE_MARIJUANA SEX_W_DRG_ETOH__NEVER_
159! SEX_W_NON_INJ_DRUG_USER SEX_W_DRG_ETOH_MOST_TIMES_ SEX_W_DRG_ETOH_SOMETIMES
159! SEX_W_DRG_ETOH_MOST_TIMES
160 SEX_W_DRG_ETOH__ALWAYS_ _06D_HAD_AN_STD_IN_PAST_3Y _06B_HAD_GC_IN_PAST_3_YRS
160! A_NO_PARTNERS_IN_90_DAYS A__2_PARTNERS_IN_90_DAYS NEW_PARTNER_LAST_VISIT
160! SEX_W_PRTNR_MET_INTERNET
161 A__1_PARTNER_IN_90_DAYS _04_SEX_WHILE_USING_NON_ID A__4_9_PARTNRS_IN_90_DAYS
161! RECTAL_INTERCOURSE ECSTACY__METHAMPHETAMINES
162 _10_SEX_PARTNERS_90_DAYS DRUG_USE_METHAMPHETAMINES SEX_W_TRANSGENDER _07_SEX_W_IDU
162! COCAINE_USE_SNORT _03_USING_USED_INJ_DRUGS
163 DRUG_USE_HEROIN COCAINE_USE_INJECT DRUG_USE_OPIOIDS SEX_FOR_DRUGS_MONEY GENDER_SP;
ERROR: Too few variables defined for the dimension(s) specified for the array charn.
164 array charc(41) $12. PATIENT_ID1 - GENDER_SP41;
ERROR: Alphabetic prefixes for enumerated variables (PATIENT_ID1-GENDER_SP41) are different.
ERROR: Too few variables defined for the dimension(s) specified for the array charc.
165 do i = 1 to 41;
166 charc(i)=put(charn(i),z11.);
----
48
ERROR 48-59: The format $Z was not found or could not be loaded.
167 end;
168 run;
NOTE: Character values have been converted to numeric values at the places given by:
(Line):(Column).
166:1
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.WANT may be incomplete. When this step was stopped there were 0
observations and 41 variables.
WARNING: Data set WORK.WANT was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.50 seconds
cpu time 0.03 seconds
You can't change the data type of a variable within a SAS data step. You need to create a new variable and assign the old variable while converting it (i.e. using a put function).
You also can't reference a list of variable in an array the way you do it. You can use the automatic variable _character_ to reference all character variables at once.
If you search the communities here a bit then you'll find quite a few discussions around conversion of variables from numeric to character.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.