BookmarkSubscribeRSS Feed
Dhana18
Obsidian | Level 7
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

 

1 REPLY 1
Patrick
Opal | Level 21

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.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 963 views
  • 0 likes
  • 2 in conversation