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

Hi,

 

Do we have any function in proc sql which is equivalent to "vtype" in Data step?. This is to check whether the field is a character or numeric and based on that passing it through a condition.

For example:

(Case when vtype(field) = 'C' then condition1 else condition2 end)

 

I appreciate any thoughts and suggestions on this. Thanks, DU

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

You can make a macro variable to hold this:


data check_v1;
infile datalines dsd missover dlm="#";
input var1 $ var2 $ var3 var4 var5;
datalines;
ND# ND#100#11#13
1500#ND#100#23#53
ND #1000#200#934#598
ND#ND #200#433#624
1000#2000#300#548#6889
1000#1000#300#548#6859
1000#2000#100#872#638
2000#3000#200#54987#879
2000#3000#200#549#548
;
RUN;
%MACRO CH(VAR);
PROC SQL;
select case when type='char' then "STRIP(&VAR.)" else "&VAR." end as temp length=40
 into : condition
from dictionary.columns 
where libname='WORK' and memname='CHECK_V1' and upcase(name)="%upcase(&var)";

CREATE TABLE CHECK_V2_&VAR. AS
SELECT &condition AS GROUP,
SUM(VAR3) AS SUM3,
SUM(VAR4) AS SUM4
FROM CHECK_V1
GROUP BY 1;
QUIT;
%MEND;

%CH(VAR1)
%CH(VAR2)
%CH(VAR3)





View solution in original post

7 REPLIES 7
jklaverstijn
Rhodochrosite | Level 12

If only vtype would work in SQL.

 

But maybe this is a way?

proc sql;
select 
case when (select type from dictionary.columns where libname='SASHELP' and memname='CLASS' and name='Name') = 'char' then 'CHAR' else 'NUM' end as coltype
from sashelp.class;
run;

 

Will get tedious with many variables but it does get the job done. May you can macro-fy this to make it more shorter.

 

Hope this helps,

- Jan.

 

 

Data_User
Quartz | Level 8

@jklaverstijn I am sorry, I am not able to understand it. Can you please suggest how can I achieve it from the below code?

 

data check_v1;

infile datalines dsd missover dlm="#";

input var1 $ var2 $ var3 var4 var5;

datalines;

ND# ND#100#11#13

1500#ND#100#23#53

ND #1000#200#934#598

ND#ND #200#433#624

1000#2000#300#548#6889

1000#1000#300#548#6859

1000#2000#100#872#638

2000#3000#200#54987#879

2000#3000#200#549#548

;

RUN;

 

 

%MACRO CH(VAR);

PROC SQL;

CREATE TABLE CHECK_V2_&VAR. AS

SELECT (case when vtype(&VAR.)='C' then STRIP(&VAR.) else &VAR. end) AS GROUP,

SUM(VAR3) AS SUM3,

SUM(VAR4) AS SUM4

FROM CHECK_V1

GROUP BY 1;

QUIT;

%MEND;

%CH(VAR1);

%CH(VAR2);

%CH(VAR3);

 

Thanks

DU

ChrisNZ
Tourmaline | Level 20

You could use

select cats(&VAR.) as GROUP

if you dont mind always making GROUP a string

Data_User
Quartz | Level 8

Thanks for contributing on this. I really appreciate it.

Reeza
Super User

Rreate a table with the required formats and use PUTC instead.

You can then use a data step to get the types or a query to the Dictionary tables. 

Ksharp
Super User

You can make a macro variable to hold this:


data check_v1;
infile datalines dsd missover dlm="#";
input var1 $ var2 $ var3 var4 var5;
datalines;
ND# ND#100#11#13
1500#ND#100#23#53
ND #1000#200#934#598
ND#ND #200#433#624
1000#2000#300#548#6889
1000#1000#300#548#6859
1000#2000#100#872#638
2000#3000#200#54987#879
2000#3000#200#549#548
;
RUN;
%MACRO CH(VAR);
PROC SQL;
select case when type='char' then "STRIP(&VAR.)" else "&VAR." end as temp length=40
 into : condition
from dictionary.columns 
where libname='WORK' and memname='CHECK_V1' and upcase(name)="%upcase(&var)";

CREATE TABLE CHECK_V2_&VAR. AS
SELECT &condition AS GROUP,
SUM(VAR3) AS SUM3,
SUM(VAR4) AS SUM4
FROM CHECK_V1
GROUP BY 1;
QUIT;
%MEND;

%CH(VAR1)
%CH(VAR2)
%CH(VAR3)





Data_User
Quartz | Level 8

Awesome. It's working. Thanks very much for this 🙂 Really appreciate it..

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 7 replies
  • 6714 views
  • 4 likes
  • 5 in conversation