Hi,
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
Best regards
Nikos
There is a function you can use to check whether it is character or numeric.
array x(4) x1-x4;
y=vtype(x(1));
put y=;
Ksharp
do you want to try something like below? you can convert it to numeric without knowing it is a character or numeric variable.
data have;
input a$ b;
_a=a*1;_b=b*1;
cards;
1 2
2 3
;
proc contents data=have;run;
Linlin
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.
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.
There is a function you can use to check whether it is character or numeric.
array x(4) x1-x4;
y=vtype(x(1));
put y=;
Ksharp
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.
data _null_;
c='c';
n=1;
tc=vtype(c);
tn=vtype(n);
put 'tc=' tc 'tn=' tn;
run;
Regards,
Haikuo
This example is from documentation. I Just copy it here.
try something like this....
data have;
cnt_sc=1;
cnt_nc="1";
cnt_ga="1";
cnt_fl=1;
output;
run;
proc contents data=have out=_type noprint;run;
procsql noprint;
select
name,
"_"||strip(name),
"_"||strip(name)||"="||strip(name)
into
:cntChar separated by " ",
:NcntChar separated by" ",
:RcntChar separated by" "
from _type wheretype=2;
quit;
%put &RcntChar;
data want(drop=&cntChar rename=(&RcntChar));
set have;
array _c &cntChar;
array _n &NcntChar;
do over _c;
_n=input(_c,8.);
end;
run;
proc contentsdata=want; run;
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.
DATA test;
input c_num $;
datalines;
123
12a4
12)1
23 1
-43
-.5
;
run;
data test1 ;
set test ;
ck_num = input(c_num,??8.) ;
run ;
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
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.
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.