DATA Step, Macro, Functions and more

Applying SAS functions on variables based on data types

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

Applying SAS functions on variables based on data types

Hello Members,

 

Hope all is well.

 

I need some help on an issue that I am facing on SAS.

 

Problem Statement:

 

I need to apply various character functions on multiple columns from multiple tables, where columns are character and numeric. Now, as character functions will not work on numeric variables, I will convert them to character as well. As I will have a loop in which variables will appear one by one inside the functions, I need a way to segregate the variables, i.e. char variable will go to char processing block and numeric variable will go to numeric processing block. I am trying to write a program which will send variables to their respective processing block, but unable to do so based on their type. How can I attach their data type in the code? I have looked at dictionary.columns, but could not do it.

 

Example: I would like to see whether the columns have negative (-) sign in them.

 

Table            Column       Type

Tab_M            H_A            Char

Tab_M            H_B             Num

 

Tab_N            G_A            Char

Tab_N            G_B             Num

 

Index(..., '-') works on Character variable, and Index(Put(...), w.), '-') works on Numeric variable. But I need to send variables to the respective blocks dynamically, based on their data type.

 

Part of code:

 

Case when type='char' then

  case when index(..., '-') then 1

  else 0

   end

 

Case when type='num' then

 case when index(put(...)) then 1

  else 0

  end

Else 0

End as Negative_Check

 

 

Thank you in advance.

 


Accepted Solutions
Solution
‎10-05-2015 12:52 AM
Super User
Posts: 3,256

Re: Applying SAS functions on variables based on data types

I suggest you check out the VTYPE function as documented here:

 

http://support.sas.com/documentation/cdl/en/lefunctionsref/67960/HTML/default/viewer.htm#n161ybp2fgd...

 

VTYPE(varname) returns the value C if varname is character or N if varname is numeric.

View solution in original post


All Replies
Solution
‎10-05-2015 12:52 AM
Super User
Posts: 3,256

Re: Applying SAS functions on variables based on data types

I suggest you check out the VTYPE function as documented here:

 

http://support.sas.com/documentation/cdl/en/lefunctionsref/67960/HTML/default/viewer.htm#n161ybp2fgd...

 

VTYPE(varname) returns the value C if varname is character or N if varname is numeric.

Respected Advisor
Posts: 4,927

Re: Applying SAS functions on variables based on data types

Restriction: VTYPE() only works in the DATA step.
PG
Contributor
Posts: 39

Re: Applying SAS functions on variables based on data types

SASKiwi,

 

Thank you very much.

 

While I am trying to understand how do I define the dataset for VTYPE, I found another function called VARTYPE.

 

https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000148443.htm

 

Here is the code that works:

 

If vartype(open("libref.dataset,"i"), varnum(open("libref.dataset,"i"),"Variable_1"))="C" then do;

         if index("Variable_1",'-') > 0  Then output temp;

 

Else if vartype(open("libref.dataset,"i"), varnum(open("libref.dataset,"i"),"Variable_1"))="N" then do;

       

Super User
Posts: 3,256

Re: Applying SAS functions on variables based on data types

VTYPE applies to any variable defined in a DATA step. There is no need to predefine any dataset it belongs to:

 

data example;

  set sashelp.class;

  if vtype('age') = 'N' then .....

run;

Valued Guide
Posts: 765

Re: Applying SAS functions on variables based on data types

Hi, one idea ...

 

Always use a CAT function (both for character and numeric) and just search for characters.  Here's a data step example, should also work in SQL, don't have to check for type ...

 

data x;
input x1 x2 :$5.;
datalines;
55255 A-AAA
66662 BBB-B
99999 CCCCC
;

data y;
set x;
if index(cat(x1),'2') then found_num = 1; else found_num = 0;
if index(cat(x2),'-') then found_char = 1; else found_char = 0;
run;

 

No problems in the LOG ...

 

38   data y;
39   set x;
40   if index(cat(x1),'2') then found_num = 1; else found_num = 0;
41   if index(cat(x2),'-') then found_char = 1; else found_char = 0;
42   run;

NOTE: There were 3 observations read from the data set WORK.X.
NOTE: The data set WORK.Y has 3 observations and 4 variables.

 

and it works ...


                         found_    found_
Obs      x1      x2        num      char

 1     55255    A-AAA       1         1
 2     66662    BBB-B       1         1
 3     99999    CCCCC       0         0

 

Valued Guide
Posts: 765

Re: Applying SAS functions on variables based on data types

[ Edited ]

Hi, just curious as to why you would still want to check for variable type when you can use the INDEX function for either NUMERIC or CHARACTER variables if you use the CAT function within the INDEX function ... INDEX(CAT(var), "search string").  Seems as if checking for variable type with VTYPE is extra effort.

Contributor
Posts: 39

Re: Applying SAS functions on variables based on data types

Hello Mike,

 

I was checking the datatype of the variable,i.e. whether it is Char or Numeric. Based on that, I was doing some extra operation with if-else.

Valued Guide
Posts: 765

Re: Applying SAS functions on variables based on data types

Hi, from your posting, it looked as if the extra stuff was using an INDEX function.  I was trying to point out that you can use any SAS character function with numeric variables if you just use the CAT function with the numeric variables.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 320 views
  • 2 likes
  • 4 in conversation