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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

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

8 REPLIES 8
SASKiwi
PROC Star

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.

PGStats
Opal | Level 21
Restriction: VTYPE() only works in the DATA step.
PG
zoomzoom
Obsidian | Level 7

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;

       

SASKiwi
PROC Star

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;

MikeZdeb
Rhodochrosite | Level 12

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

 

MikeZdeb
Rhodochrosite | Level 12

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.

zoomzoom
Obsidian | Level 7

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.

MikeZdeb
Rhodochrosite | Level 12

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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