04-18-2012 08:19 AM
I have a macro that calls several data sets at certain step i use a proc transpose that creates a new variable as follows CNT_&STATE with &STATE (macro variable)and taking values like AON, KAT etc. depending on the input of the initial data set used.
Subsequently the output data set of the proc transpose is further developed but at this stage I would like to:
a) check whether var CNT_&STATE is a character or numeric var
b) if it is character var then to change it to numeric
Any suggestions more than welcome
Thank you in advance
04-18-2012 08:42 AM
do you want to try something like below? you can convert it to numeric without knowing it is a character or numeric variable.
input a$ b;
proc contents data=have;run;
04-18-2012 09:07 AM
And to end up with the same names you started with:
data have (drop=a b rename=(_a=a _b=b);
This approach can give you notes in the log about character to numeric conversion. If those notes are troublesome to you, there are more complex ways to solve the problem.
04-18-2012 10:35 AM
It would be easier to check the variable being transposed and convert it to numeric before it is transposed. Then there is no rename issue. Post some sample data and I will show you what I'm thinking about.
04-19-2012 08:28 AM
Excellent idea, but with a little confusing example, as you should already know the variable type of your array members before you can define them.
put 'tc=' tc 'tn=' tn;
04-19-2012 09:58 AM
try something like this....
proc contents data=have out=_type noprint;run;
:cntChar separated by " ",
:NcntChar separated by" ",
:RcntChar separated by" "
from _type wheretype=2;
data want(drop=&cntChar rename=(&RcntChar));
array _c &cntChar;
array _n &NcntChar;
do over _c;
proc contentsdata=want; run;
04-19-2012 12:01 PM
If you use var_nm = input(c_num,??8.); it will put a number when it is numeric and . when it is not. Here is an example.
input c_num $;
data test1 ;
set test ;
ck_num = input(c_num,??8.) ;
This gives the following log and proc print; output.
NOTE: There were 8 observations read from the data set WORK.TEST.
NOTE: The data set WORK.TEST1 has 8 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
The SAS System 13:59 Monday, April 16, 2012 1
Obs c_num ck_num
1 123 123.00
2 12.34 12.34
3 12.34.5 .
4 12a4 .
5 12)1 .
6 23 23.00
7 -43 -43.00
8 -.5 -0.50
04-19-2012 12:37 PM
Exactly and without error messages.
From my understanding of the task, "a) check whether var CNT_&STATE is a character or numeric var", was refering to the variable, not its contents specifically as often times character viarbles only contain numeric data. Also, "b) if it is character var then to change it to numeric", by default, if you change a character field to a numeric, you will lose any non-numeric data as it is nolonger compatible in the new datatype.
On the other hand, if he only wanted to change those from character to numeric where it could without data loss, then you could do another check prior to the sql statemes above to trap the character variables that you didn't want converted.
04-20-2012 09:31 AM
Right, I skipped the answer to the first part of the question. Fortunately there is a very simple solution.
Use the vtype function to test if CNT_&STATE is charater or numeric.
vtype(cnt_&state); Returns 'N' if it is numeric and 'C' if it is character.
Need further help from the community? Please ask a new question.