BookmarkSubscribeRSS Feed
RTelang
Fluorite | Level 6
56 options nosymbolgen nomprint;
57 %macro dataatt(data = ,attribute =,type=,notfound = );
58 %local dsid;
59 %if %length(&type) = 0 %then %let type = N;
60 %else %let type = %upcase(%substr(&type,1,1));
61
62 %if &type = N %then %let notfound = .;
63 %else %let notfound = ' ';
64
65 %let dsid = %sysfunc(open(&data));
66 %if &dsid ne 0 %then %sysfunc(attrn(&dsid,&attribute));
67 %else &notfound;
68
69 %let dsid = close(&dsid);
70
71 %mend dataatt;
72 %dataatt(data =sashelp.class,attribute=age,type=n,notfound = );
WARNING: Argument 2 to function ATTRN 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.
NOTE: Line generated by the macro function "SYSFUNC".
72 .
_
180
 
ERROR 180-322: Statement is not valid or it is used out of proper order.  
 
 
how to rectify d code?
15 REPLIES 15
mohamed_zaki
Barite | Level 11

66 %if &dsid ne 0 %then %sysfunc(attrn(&dsid,&attribute));

 

What you try to do in this line?

You are using attrn with wrong attr-name ? chk : ATTRN Function valid attr-name

Where is varnum in your code?

In which variable you will keep the %sysfunc(); values? I think you are missing a %let in this line.....

RTelang
Fluorite | Level 6
@zaki oops my bad sorry varnum i wrote by mistake, am trying to extract a attribute of a macro variable & displaying it in d log.
RTelang
Fluorite | Level 6
and having issue with
%if &dsid ne 0 %then %sysfunc(attrn(&dsid,&attribute));
& having issue solving this errorthis attarn
mohamed_zaki
Barite | Level 11

If you are going to use ATTRN Function

Then you need to use valid attr-name with it in the second argument like  NOBS, ANY ........ other mentioned on the link above

 

 

So for example your code may look like:

 

%if &dsid ne 0 %then %put %sysfunc(attrn(&dsid,nobs));

 

 

 

dcruik
Lapis Lazuli | Level 10

I also think once you fix the attribute parameter in the attrn() function, you will get a syntax error at line 67 with "%else &notfound;".  Are you trying to put the notfound macro variable into another variable, or are you trying to print the value of the notfound macro variable to the log?  If you leave it as is, the code will resolve to %else . or %else ' ', depending on the value of the type macro variable.  SAS will read this and give you a syntax error. 

 

If you are wanting to print the value of the notfound macro variable to the log, you will need to add a "%put" in front of the &notfound.  If you are wanting to put the value of the notfound macro variable into another variable or a data set, you will need to add another %let statement setting a variable equal to &notfound or have a data step with the variable notfound put into a field.  Hope this helps!

RTelang
Fluorite | Level 6
%macro datatt(data = ,attribute =,type =,notfound = );
%local dsid;

%if %length(&type)= 0 %then %let type= N;
%else %let type = %upcase(%substr(&type,1,1));
%if &type = N %then %let notfound = .;
%else %let notfound = ' ';

%let dsid = %sysfunc(open(&data));
%if &dsid ne 0 %then
%put %sysfunc(attrn(&dsid,nobs));
%else
%put &notfound;
%let dsid = close(&dsid);

%mend datatt;
%datatt(data =work.test0,attribute =var3,type =N,notfound =.); hello this is my code but it doesn't display variable attribute what changes do i do to display a attribute of a specified dataset variable.
dcruik
Lapis Lazuli | Level 10

When you say "variable attribute", do you mean the variable type (Character/Numeric), Length, Format, etc..?  You could always run a proc contents and output it to a data set and limit that data set by the NAME value for whatever variable you are looking for.  Then you could set some macro variables equal to some of the different values in the data set for that variable and print those to the log.  Is that something you are trying to accomplish?

RTelang
Fluorite | Level 6
nope i want to display att of data set variable & not the type of variable... using a generic macro
RTelang
Fluorite | Level 6
My requirement--->need a macro code to return an attribute of a specified data-set variable,
My code only giving the number of attribute in d log how can i modify it to display a specific attribute of a specified data-set variable....?
%macro datatt(data = ,
attribute =,
type=);
%local dsid;

%if %length(&type)= 0 %then %let type= N;
%else %let type = %upcase(%substr(&type,1,1));
%if &type = N %then %let notfound = .;
%else %let notfound = ' ';

%let dsid = %sysfunc(open(&data));
%if &dsid ne 0 %then
%put %sysfunc(attrn(&dsid,nobs));
%else
%put &notfound;
%let dsid = close(&dsid);

%mend datatt;
dcruik
Lapis Lazuli | Level 10

So, using the attrn() function, there are many different "attr-names" you can use in the second argument of the function.  You currently have nobs hardcoded to return which specifies the number of physical observations.  You could add an additional parameter in your %macro statement to set whatever attr-name you want to use with the attrn() function.  The statement would change to "%macro datatt(data=, attribute=, type=, notfound=, attrname=);".  All the different types of attributes you can find at the following link:  http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212040.htm


I would modify the nobs in the second argument of the attrn() function and set it equal to "&attrname".  Then in your call of the datatt macro, if you wanted to display nobs, you just call it like "%datatt(data=sashelp.class, attribute=age, type=n, notfound=, attrname=nobs);".

RTelang
Fluorite | Level 6
@dcruik applied ur logic but got a error

56 %macro datatt(data = ,
57 attribute =,
58 type=);
59 %local dsid;
60
61 %if %length(&type)= 0 %then %let type= N;
62 %else %let type = %upcase(%substr(&type,1,1));
63 %if &type = N %then %let notfound = .;
64 %else %let notfound = ' ';
65
66 %let dsid = %sysfunc(open(&data));
67 %if &dsid ne 0 %then
68 %put variable=%sysfunc(attrn(&dsid,&&attrname));
69 %else
70 %put &notfound;
71 %let dsid = close(&dsid);
72
73 %mend datatt;
74 %datatt(data=sashelp.class, attribute=age, type=n, notfound=., attrname=nobs);
ERROR: The keyword parameter NOTFOUND was not defined with the macro.
ERROR: The keyword parameter ATTRNAME was not defined with the macro.
dcruik
Lapis Lazuli | Level 10

Change the "&&attrname" to "&attrname".  Also, you need to include the notfound and attrname parameters in the %macro statement.  Instead of "%macro datatt(data=,attribute=,type=);", try doing "%macro datatt(data=,attribute=,type=,notfound=,attrname=);".  That should fix those errors.

RTelang
Fluorite | Level 6
@dcruik i think my code is wrong i do not want to display numeric attributes of a variable but i want to display the variable itself.

56 %macro datatt(data=,
57 attribute=,
58 type=,
59 notfound=,
60 attrname=);
61 %local dsid;
62
63 %if %length(&type)= 0 %then %let type= N;
64 %else %let type = %upcase(%substr(&type,1,1));
65 %if &type = N %then %let notfound = .;
66 %else %let notfound = ' ';
67
68 %let dsid = %sysfunc(open(&data));
69 %if &dsid ne 0 %then
70 %put variable=%sysfunc(attrn(&dsid,&attrname));
71 %else
72
73 %put &notfound;
74 %let dsid = close(&dsid);
75
76 %mend datatt;
77 %datatt(data=sashelp.class, attribute=, type=n, notfound=., attrname=nvars);
variable=5--->
a specific variable name should be displayed..
RTelang
Fluorite | Level 6
can we edit this code to display attribute of the dataset variable...

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
  • 15 replies
  • 2131 views
  • 0 likes
  • 3 in conversation