DATA Step, Macro, Functions and more

Stop Automatic Variable Conversion

Reply
Contributor
Posts: 20

Stop Automatic Variable Conversion

Hi, I am using SAS to clean survey data. There are two versions of the survey (one in English and one in Spanish), and the goal here is to "convert" Spanish variables to English variables. I've written macros to assign the English variables the values of their Spanish counterparts. Everything works fine for numeric values, but I get this following message when dealing with character values: 

 

NOTE: Character values have been converted to numeric values

 

Is there any way to stop this automatic conversion and simply do "English var = Spanish var"? Thank you.

Super User
Posts: 11,343

Re: Stop Automatic Variable Conversion

It really helps to show at least some example data when requesting data manipulation tips. Show a few examples for some variables from each data set and what the result should be.

It appears that you have two data sets. You may need to check between the two if the variables are the same type (proc contents for example) as it appears you may have that issue from the message. If that is the case you should describe how the data was brought into SAS. Hint: Proc Import guesses as to varaible type and depending on the source file type (Excel is about the worst) and options the guesses may not be very good.

 

Also you should show the code that generated the message. You will get this if you do:  If Var = 9 then (do something) if Var is character. The fix would be If Var='9' then (do something).

Contributor
Posts: 20

Re: Stop Automatic Variable Conversion

[ Edited ]

Hi, ballardw. Thank you for the reply.

 

This is my macro:

 

%MACRO spantoengother(dataset=, numofvars=, firstvareng=, lastvareng=, firstvarspan=, lastvarspan=);
data &dataset;
	set &dataset;
	array areverseeng {&numofvars} &firstvareng--&lastvareng;
	array areversespan {&numofvars} &firstvarspan--&lastvarspan;
	do i=1 to dim(areversespan);
		if areversespan{i} ne . then areverseeng[i] = areversespan[i];
        end;
run;
%MEND spantoengother;

I know that variables in an array have to be the same type, so I made sure that the variables are the same type. For now, let's say I have a variable that contains an individual's email address (one for English and one for Spanish). I simply want to set email_eng to whatever is in email_span, if there isn't a missing value.

Super User
Posts: 19,789

Re: Stop Automatic Variable Conversion

For a character variable you need to have an array declared with $ in it, otherwise it defaults to numeric. 

 

And missing in character variables is " ". If you want the code to work for both numeric or character using MISSING() function instead.

 

	array areverseeng {&numofvars} $ &firstvareng--&lastvareng;
	array areversespan {&numofvars} $  &firstvarspan--&lastvarspan;
	do i=1 to dim(areversespan);
		if not missing(areversespan[i]) then areverseeng[i] = areversespan[i];
        end;
Contributor
Posts: 20

Re: Stop Automatic Variable Conversion

I will try that. Thank you!
Contributor
Posts: 20

Re: Stop Automatic Variable Conversion

Hi, Reeza. I tried this, but it did not work. I'm getting "999"'s instead of the email address. Weird.
Super User
Posts: 19,789

Re: Stop Automatic Variable Conversion

Are the 999s missing in the character variables? or are they coded in some other manner?

Contributor
Posts: 20

Re: Stop Automatic Variable Conversion

They're missing, but for one of the observations that I looked at, what was inputted for the Spanish variable was not transferred to its English counterpart. No idea why.
Super User
Posts: 19,789

Re: Stop Automatic Variable Conversion

With what you've posted I can't guess either Smiley Happy

If you can't post actual data to replicate your issue you'll need to contact tech support.

Contributor
Posts: 20

Re: Stop Automatic Variable Conversion

Ugh, guess that's the only solution, as it contains personal identifying information. Thanks anyway! Smiley Happy
Contributor
Posts: 20

Re: Stop Automatic Variable Conversion

Hmm...it seems to not work for only ONE of the observations, as it works for the others...
Super User
Posts: 19,789

Re: Stop Automatic Variable Conversion

Then there's something specific about that observation.

You should be able to strip the identifying information and replicate the error.

 

Filter out that specific obs. Then check if it passes the missing criteria or not.

Super User
Posts: 11,343

Re: Stop Automatic Variable Conversion

Are you seeing any messages in the log about encoding such a UTF-8 and WLATIN or other strangeness.

Other option is that some of the data may actually be UNICODE characters. They might look like "999" but are actually represented by different characters that are different than the ones you enter in the editor.

Ask a Question
Discussion stats
  • 12 replies
  • 292 views
  • 0 likes
  • 3 in conversation