BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
zaldarsa
Obsidian | Level 7
Proc format;
value  quartile 0= 'Q1'
		        1= 'Q2'
				2= 'Q3'
				3= 'Q4';
value educ      1-4='< High School'
                5= 'High School/GED'
				6-7 = '>High school < 4-year college'
				8-11= ' >=4-year college';
value race     1= 'American Indian/Alaskan Native'
               2= 'Asian'
			   3= 'Native Hawaiian/Other'
			   4= 'Black'
			   5= 'White'
			   6= 'Multiple'
			   9='Unknown/Not Reported';
value smoke  0= 'Never Smoked'
             1= 'Past Smoker'
			 2= 'Current Smoker';
value hrt   1= 'E-Alone HT'
            2= 'E- Alone Placebo'
            3= ' E+P HT'
            4= 'E+ P Placeb';
value exercise
            1= 'No Activity'
			2='Some Activity of Limited Duration'
			3='2 to <4 Episodes per Week'
            4= '>= Episodes per week';
value cat 
            1= 'Yes'
			2= 'No'
            .= 'Missing';

value difcat  0= 'No'
            1= 'Yes'
			.= 'Missing';
run;


*Converting numeric values to character values;

data mri1;
set mri2 ;
array var3 rvfat_area_corr rsat_area_corr rbmi rtotal_pfat rtotal_plean;
do i=1 to dim(var3);
var3(i)= put(var3(i),quartile.);
end;
array var1 nswhitematter sevwhitematter mildatrophy sevatrophy bppills anticoag;
do i=1 to dim(var1);
var1(i)= put(var1(i), cat.);
end;
array var2 diab diabtrt cvd hypt horm;
do i=1 to dim(var2);
var2(i)= put(var2(i), difcat.);
end;
new_educ=put(educ, educ.);
race= put(racenih, race.);
new_smoke= put(smoking,smoke.);
hrtassign= put(hrtarm, hrt.);
exercise= put(lmsepi, exercise.);
run;

Hello! I want to convert several numeric variables into character variables. I thought it would be practical to use arrays for the ones that I want to format in the same way. However, the array does not appear to be working. This message appears in my log:  Character values have been converted to numeric values at the places given by:
(Line):(Column).
181:1 185:1 189:1
NOTE: Invalid numeric data, 'Q3' , at line 181 column 10.   and several other places as well. I was wondering what I was doing incorrectly when i created the arrays. Thank you in advance for your help!!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You defined the variables as NUMERIC.  So you cannot put the character string the PUT() function generates into them.

If you want to attach the formats to the existing numeric variables just use a FORMAT statement.

data mri1;
  set mri2 ;
  format 
    rvfat_area_corr rsat_area_corr rbmi rtotal_pfat rtotal_plean quartile.
    nswhitematter sevwhitematter mildatrophy sevatrophy bppills anticoag cat.
    diab diabtrt cvd hypt horm difcat.
  ;
run;

If you want to make character variables then you need to make NEW variables, like you did for the others.

array var3 rvfat_area_corr rsat_area_corr rbmi rtotal_pfat rtotal_plean;
array var3F $2 new_rvfat_area_corr new_rsat_area_corr new_rbmi new_rtotal_pfat new_rtotal_plean;
do i=1 to dim(var3);
  var3f[i]= put(var3[i],quartile.);
end;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

You defined the variables as NUMERIC.  So you cannot put the character string the PUT() function generates into them.

If you want to attach the formats to the existing numeric variables just use a FORMAT statement.

data mri1;
  set mri2 ;
  format 
    rvfat_area_corr rsat_area_corr rbmi rtotal_pfat rtotal_plean quartile.
    nswhitematter sevwhitematter mildatrophy sevatrophy bppills anticoag cat.
    diab diabtrt cvd hypt horm difcat.
  ;
run;

If you want to make character variables then you need to make NEW variables, like you did for the others.

array var3 rvfat_area_corr rsat_area_corr rbmi rtotal_pfat rtotal_plean;
array var3F $2 new_rvfat_area_corr new_rsat_area_corr new_rbmi new_rtotal_pfat new_rtotal_plean;
do i=1 to dim(var3);
  var3f[i]= put(var3[i],quartile.);
end;
zaldarsa
Obsidian | Level 7

Thank you! I follow your second suggestion, and it worked fine! 

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
  • 2 replies
  • 594 views
  • 1 like
  • 2 in conversation