code to state the datatype of the variable in a dataset

Reply
Regular Contributor
Posts: 190

code to state the datatype of the variable in a dataset

hello i am trying to display numeric or character variable in a particular data set but having issues wats warong!!1 thanks in advance

 

 

 
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
55
56 %macro vtype(dsname=, varname=);
57 %let dsid = %sysfunc(open(&dsname));
58 %let varnum = %sysfunc(varnum(&dsid,&varname));
59 %let vartyp = %sysfunc(vartype(&dsid,&varnum));
60 %if &vartyp = C %then %do;
61 %put &vartyp=character;
62 %end;
63 %else %if &vartyp = N %then %do;
64 %put &vartyp=numeric;
65 %end;
66 %mend vtype;
67 %vtype;
WARNING: Argument 2 to function VARNUM referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.
NOTE: Mathematical operations could not be performed during %SYSFUNC function execution. The result of the operations have been set
to a missing value.
WARNING: Argument 2 to function VARTYPE referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.
68
Trusted Advisor
Posts: 1,137

Re: code to state the datatype of the variable in a dataset

The reason for getting the WARNINGS is that either the dataset name or the variable you are passing into the parameters dsname and varname is not there.

 

Else the macro is working well.

 

Please check if the dsname i mean the dataset you are passing is there in work or any library and also make sure that variable name you are passing is also there in the dataset.

Thanks,
Jag
Super User
Super User
Posts: 7,942

Re: code to state the datatype of the variable in a dataset

This really is the same question as the numerous other questions posted here.  The answers to your syntax errors will be the same.

 

For your reference on what you are trying to do, I attach screenshots to the SAS metadata datasets, which contain all the information you need, are automatically created and maintained by the SAS system, and are widely recognised and understood by SAS programmers.

SASHELP.VCOLUMNS (or DICTIONARY.COLUMNS in SQL): 

 

Capture.PNG

 

SASHELP.VTABLE (or DICTIONARY.TABLES in SQL):

Capture2.PNG

Trusted Advisor
Posts: 1,117

Re: code to state the datatype of the variable in a dataset

@RW9: Telling from RTelang's previous posts, he (or she) is not working on a specific task, but learning macro programming just for the sake of it.

 

@RTelang: The log excerpt that you present is incomplete and not consistent: Running only the code that is shown, one would obtain the following log messages:

WARNING: Argument 1 to function VARNUM referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.
WARNING: Argument 1 to function VARTYPE referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.

 

The NOTE would be suppressed by the NONOTES option and the WARNINGs would refer to "Argument 1", because your macro call "%vtype;" does not pass any parameter values to the macro. I think you should reread the documentation on macro parameters (see %MACRO Statement) to improve your understanding.

 

I conclude you have created a global macro variable DSNAME (probably by using a %LET statement not shown in your log excerpt) and stored the name of an existing dataset in it. Therefore, macro variable DSID received a valid dataset ID and only &VARNAME did not resolve to a valid variable name in the dataset mentioned (hence the issues with "Argument 2").

 

Creating a global macro variable with the same name as the parameter of a macro can easily cause confusion. If you had correctly specified the parameter values in your macro call (e.g. %vtype(dsname=sashelp.class, varname=age)), the content of the global macro variable DSNAME would have been ignored ("shadowed" by the parameter value stored in the local macro variable DSNAME) during macro execution.

 

Finally, if you use the OPEN function, please don't forget to close the opened datasets by using the CLOSE function at the end. Otherwise, people can no longer overwrite or delete the affected datasets.

Regular Contributor
Posts: 190

Re: code to state the datatype of the variable in a dataset

Posted in reply to FreelanceReinhard
@FreelanceReinhard:- i am doing tasks & am new to this forum. i hav sas university edition so getting used to it, Secondly i have already done my code i did not send it yesterday here am posting it.

%macro vtype(dsname=, varname=);
%let dsid = %sysfunc(open(&dsname));
%let varnum = %sysfunc(varnum(&dsid,&varname));
%let vartyp = %sysfunc(vartype(&dsid,&varnum));
%if &vartyp = C %then %do;
%put &varname=character;
%end;
%else %if &vartyp = N %then %do;
%put &varname=numeric;
%end;
%mend vtype;
%vtype(dsname=sashelp.class, varname=sex);
Ask a Question
Discussion stats
  • 4 replies
  • 783 views
  • 0 likes
  • 4 in conversation