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.
I suggest you check out the VTYPE function as documented here:
VTYPE(varname) returns the value C if varname is character or N if varname is numeric.
I suggest you check out the VTYPE function as documented here:
VTYPE(varname) returns the value C if varname is character or N if varname is numeric.
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;
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;
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
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.
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.