BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nikos
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

View solution in original post

10 REPLIES 10
Linlin
Lapis Lazuli | Level 10

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

Astounding
PROC Star

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.

data_null__
Jade | Level 19

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. 

Ksharp
Super User

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

Haikuo
Onyx | Level 15

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

Ksharp
Super User

This example is from documentation. I Just copy it here.

byronk
Fluorite | Level 6

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;

eirhpaul
Calcite | Level 5


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

  1. 12.34
  2. 12.34.5

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

byronk
Fluorite | Level 6

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.

eirhpaul
Calcite | Level 5

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 10 replies
  • 2857 views
  • 0 likes
  • 8 in conversation