Here's a macro I'm writing, I want to look at a given data set, and write of each of the field(column) names as an observation into an info data set.
Here's what I've got
%macro go_macro(data);
/*get the list of field names from the dictionary*/
proc sql noprint;
select distinct name into :columns separated by ' '
from dictionary.columns
where memname = "%upcase(%scan(&data, -1))" and libname = "%upcase(%scan(&data, 1))" and type = "char"
;
quit;
/*now loop through this list of fieldnames*/
%let var_no = 1;
%let var = %scan(&columns, &var_no, ' ');
%do %while (&var ne);
/*here is where I want to write the fieldname to an observation*/
fname = "&var";
output;
%let var_no = %eval(&var_no +1);
%let var = %scan(&columns, &var_no, ' ');
%end;
%mend;
/*execute the code*/
data bdqa.accounts_info;
%go_macro(braw.accounts)
run;
this gives me
[MPRINT] Parsing Base DataServer
/* 0005 */ fname = "SORT_CODE";
/* 0006 */ output;
/* 0009 */ fname = "BANK_NAME";
/* 0010 */ output;
/* 0013 */ fname = "CREDIT_CARD";
/* 0014 */ output;
/* 0017 */ fname = "PRIMARY_ACCT_HOLDER";
/* 0018 */ output;
/* 0021 */ fname = "account_close_date";
/* 0022 */ output;
/* 0023 */ run;
ERROR: Parsing exception - aborting
ERROR: DS-00274 : Could not parse base DataServer code: Encountered " <ALPHANUM> "fname "" at line 5, column 9.
Was expecting one of:
<EOF>
";" ...
"*" ...
"data" ...
"proc" ...
(and 9 more)
while
while
data mytest;
do i = 1 to 5;
fname = 'hello world';
output;
end;
keep fname;
run;
is perfectly legit.
the following code
%macro char_freqs(data=);
/*get the different variables*/
proc sql noprint;
select distinct name into :columns separated by ' '
from dictionary.columns
where memname = "%upcase(%scan(&data, -1))" and libname = "%upcase(%scan(&data, 1))" and type = "char"
;
quit;
/*now get the distinct values for each of the variables*/
%let var_no = 1;
%let var = %scan(&columns, &var_no, ' ');
%do %while (&var ne);
proc freq data=&data;
tables &var/out=&var._freq;
run;
%let var_no = %eval(&var_no +1);
%let var = %scan(&columns, &var_no, ' ');
%end;
%mend;
%char_freqs(data=macros.businesses)
Also works - the PROC FREQ is allowed.
The problem is you have something like this:
data bdqa.accounts_info;
%go_macro(braw.accounts)
run;
->
data bdqa.accounts_info;
proc sql;
... select stuff ...;
quit;
fname = "stuff"; ...
run;
You need:
proc sql;
select stuff;
quit;
data bdqa.accounts_info;
fname = "stuff";
...
run;
You need to remove the PROC SQL from the macro - you can create a macro variable outside of a macro. Honestly you shouldn't use a macro for this at all - you can do one of two things:
a) Create the table directly from DICTIONARY.COLUMNS
proc sql;
create table bdqa.accounts_info as select name as fname from dictionary.columns where ... ;
quit;
b) Create it in a datastep
data bdqa.accounts_info;
__data = "&var_no";
do ... ;
run;
(the do loop is basically identical to the %do loop in the macro)
Related
I was trying to create a macro to output a list of all variables of a specific data set. In my macro, I am using PROC SQL. The code runs OK outside %macro, but error message saying the SELECT statement is not valid when it is being used within %MACRO
here is an example:
proc sql noprint;
select name into :vlist separated by ' '
from dictionary.columns
where memname = upcase("&dsn");
quit;
%put &vlist;
the above works perfectly;
but
%macro getvars(dsn);
%local vlist;
proc sql noprint;
select name into :vlist separated by ' '
from dictionary.columns
where memname = upcase("&dsn");
quit;
&vlist;
%mend;
the above doesn't work when I tried to do:
%let var_list = %getvars(dataset);
it returns:
ERROR 180-322: Statement is not valid or it is used out of proper order.
underlining the SELECT statement within the PROC SQL
SAS macros are not like functions in most programming languages: they don't return values, they are actually replaced by the content of the macro.
The solution is to make your macro variable global, outside the macro. Then you don't need to assign it to a new macro variable with %let.
%global vlist;
%macro getvars(dsn);
proc sql noprint;
select name into :vlist separated by ' '
from dictionary.columns
where memname = upcase("&dsn");
quit;
%mend;
%getvars(work.class)
%put &=vlist;
[EDIT]
and then just use the list in your keep statement
data OUT (keep= &vlist. VAR_B1);
merge DATA_A (in=a) DATA_B (in=b) ;
run;
Seems like the only viable option for my use case is from the following SAS paper, under the section of "USING A MACRO LOOP"
https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/028-30.pdf
To clarify, my use case need a direct output of the list itself, not a macro variable.
e.g.
data OUT (keep= %getvars(DATA_A) VAR_B1);
merge DATA_A (in=a)
DATA_B (in=b)
;
run;
The PROC SQL won't work for me. So I think I need to move over to SAS I/O Functions in Macro Loop.
Below is from the SAS Paper:
%Macro GetVars(Dset) ;
%Local VarList ;
/* open dataset */
%Let FID = %SysFunc(Open(&Dset)) ;
/* If accessable, process contents of dataset */
%If &FID %Then %Do ;
%Do I=1 %To %SysFunc(ATTRN(&FID,NVARS)) ;
%Let VarList= &VarList %SysFunc(VarName(&FID,&I));
%End ;
/* close dataset when complete */
%Let FID = %SysFunc(Close(&FID)) ;
%End ;
&VarList
%Mend ;
A macro using %SYSFUNC(DOSUBL( can run any amount of SAS code (in a separate stream) when invoked at source code parse-time.
Example:
data have_A;
do index = 1 to 10;
x = index ** 2; y = x-1; z = x+1; p = x/2; q = sqrt(x); output;
end;
run;
data have_B(keep=B1);
do index = 1 to 10;
B1 + index; output;
end;
run;
%macro getvars(data);
%local rc lib mem names;
%let rc = %sysfunc(DOSUBL(%nrstr(
%let syslast = &data;
%let lib = %scan (&SYSLAST,1,.);
%let mem = %scan (&SYSLAST,2,.);
proc sql noprint;
select name into :names separated by ' ' from
dictionary.columns where
libname = "&lib." and
memname = "&mem."
;
quit;
)));
/* Emit variable name list */
&names.
%mend;
data OUT (keep=%getvars(HAVE_A) B1);
merge HAVE_A (in=a) /* 1:1 merge (no BY) */
HAVE_B (in=b)
;
run;
%let var_list = %getvars(dataset);
will resolve to:
%let var_list = proc sql noprint;
select name into :vlist separated by ' '
from dictionary.columns
where memname = upcase("dataset");
quit;
So it will store "proc SQL noprint" in var_list, and then fail because you use sql satements outside of proc sql.
%let abc = ("234.34", "C56.67", "2345.67", "C67.56") ;
Output:
Table1
Col1
234.34
C56.67
2345.67
C67.56
This is throwing an error, can somebody please guide me:
%macro generate ;
%local i ;
data table1;
length Col1 $100.;
%do i=1 %to %sysfunc(countw(&abc.));
Col1 = %scan(&abc., &i.,,"sq");
output;
%end;
run;
%mend;
%generate;
you do not need a macro and one way to do this.
%let abc = ("234.34", "C56.67", "2345.67", "C67.56" );
%let abc = %qsysfunc(compress(&abc,%str(%"%)%()));
data table1;
length Col1 $100.;
do i = 1 to countw("&abc", ",");
Col1 = scan("&abc", i,',');
output;
end;
run;
You can do this with a DO loop, but you will need to remove the () from the value of the macro variable.
%let abc = ("234.34", "C56.67", "2345.67", "C67.56") ;
data table1;
length Col1 $100.;
do Col1 = %substr(&abc,2,%length(&abc)-2);
output;
end;
run;
If the values do not contain () then you could also just use %scan(&abc,1,()), which also has the advantage of working whether or not the original value has the enclosing ().
Or just remove the () from the value and add them back when you use the macro variable in place where they are needed.
%let abc = "234.34", "C56.67", "2345.67", "C67.56" ;
...
do Col1 = &abc ;
...
where dxcode in (&abc)
....
A proc sql way
%let abc = ("234.34", "C56.67", "2345.67", "C67.56") ;
proc sql;
create table tab1 (col1 varchar(100));
quit;
options macrogen mlogic;
%macro gen;
%let i = 1;
%do %while (%scan(%superq(abc),&i,%str(,)) ne %str( ));
proc sql;
insert into tab1
values(%scan(%superq(abc),&i,%str(,)));
quit;
%let i = %sysevalf(&i + 1);
%end;
%mend gen; %gen;
It would scan through your macro that is delimited by ",", and progress by 1 with each loop.
I am trying to parse a delimited dataset with over 300 fields. Instead of listing all the input fields like
data test;
infile "delimited_filename.txt"
DSD delimiter="|" lrecl=32767 STOPOVER;
input field_A:$200.
field_B :$200.
field_C:$200.
/*continues on */
;
I am thinking I can dump all the field names into a file, read in as a sas dataset, and populate the input fields - this also gives me the dynamic control if any of the field names changes (add/remove) in the dataset. What would be some good ways to accomplish this?
Thank you very much - I just started sas, still trying to wrap my head around it.
This worked for me - Basically "write" data open code using macro language and run it.
Note: my indata_header_file contains 5 columns: Variable_Name, Variable_Length, Variable_Type, Variable_Label, and Notes.
%macro ReadDsFromFile(filename_to_process, indata_header_file, out_dsname);
%local filename_to_process indata_header_file out_dsname;
/* This macro var contain code to read data file*/
%local read_code input_in_line;
%put *** Processing file: &filename_to_process ...;
/* Read in the header file */
proc import OUT = ds_header
DATAFILE = &indata_header_file.
DBMS = EXCEL REPLACE; /* REPLACE flag */
SHEET = "Names";
GETNAMES = YES;
MIXED = NO;
SCANTEXT = YES;
run;
%let id = %sysfunc(open(ds_header));
%let NOBS = %sysfunc(attrn(&id.,NOBS));
%syscall set(id);
/*
Generates:
data &out_dsname.;
infile "&filename_to_process."
DSD delimiter="|" lrecl=32767 STOPOVER FIRSTOBS=3;
input
'7C'x
*/
%let read_code = data &out_dsname. %str(;)
infile &filename_to_process.
DSD delimiter=%str("|") lrecl=32767 STOPOVER %str(;)
input ;
/*
Generates:
<field_name> : $<field_length>;
*/
%do i = 1 %to &NObs;
%let rc = %sysfunc(fetchobs(&id., &i));
%let VAR_NAME = %sysfunc(getvarc(&id., %sysfunc(varnum(&id., Variable_Name)) ));
%let VAR_LENGTH = %sysfunc(getvarn(&id., %sysfunc(varnum(&id., Variable_Length)) ));
%let VAR_TYPE = %sysfunc(getvarc(&id., %sysfunc(varnum(&id., Variable_Type)) ));
%let VAR_LABEL = %sysfunc(getvarc(&id., %sysfunc(varnum(&id., Variable_Label)) ));
%let VAR_NOTES = %sysfunc(getvarc(&id., %sysfunc(varnum(&id., Notes)) ));
%if %upcase(%trim(&VAR_TYPE.)) eq CHAR %then
%let input_in_line = &VAR_NAME :$&VAR_LENGTH..;
%else
%let input_in_line = &VAR_NAME :&VAR_LENGTH.;
/* append in_line statment to main macro var*/
%let read_code = &read_code. &input_in_line. ;
%end;
/* Close the fid */
%let rc = %sysfunc(close(&id));
%let read_code = &read_code. %str(;) run %str(;) ;
/* Run the generated code*/
&read_code.
%mend ReadDsFromFile;
Sounds like you want to generate code based on metadata. A data step is actually a lot easier to code and debug than a macro.
Let's assume you have metadata that describes the input data. For example let's use the metadata about the SASHELP.CARS. We can build our metadata from the existing DICTIONARY.COLUMNS metadata on the existing dataset. Let's set the INFORMAT to the FORMAT since that table does not have INFORMAT value assigned.
proc sql noprint ;
create table varlist as
select memname,varnum,name,type,length,format,format as informat,label
from dictionary.columns
where libname='SASHELP' and memname='CARS'
;
quit;
Now let's make a sample text file with the data in it.
filename mydata temp;
data _null_;
set sashelp.cars ;
file mydata dsd ;
put (_all_) (:);
run;
Now we just need to use the metadata to write a program that could read that data. All we really need to do is define the variables and then add a simple INPUT firstvar -- lastvar statement to read the data.
filename code temp;
data _null_;
set varlist end=eof ;
by varnum ;
file code ;
if _n_=1 then do ;
firstvar=name ;
retain firstvar ;
put 'data ' memname ';'
/ ' infile mydata dsd truncover lrecl=1000000;'
;
end;
put ' attrib ' name 'length=' #;
if type = 'char' then put '$'# ;
put length ;
if informat ne ' ' then put #10 informat= ;
if format ne ' ' then put #10 format= ;
if label ne ' ' then put #10 label= :$quote. ;
put ' ;' ;
if eof then do ;
put ' input ' firstvar '-- ' name ';' ;
put 'run;' ;
end;
run;
Now we can just run the generated code using %INCLUDE.
%include code / source2 ;
How to get minimum and maximum value of all the columns of a table? Please note that the columns may be both numeric, date or character. We have to find min and max of all the variables in following format:
Name_of_columns, minimum, maximum
Here's a macro that will do what your asking for which doesn't require you to know the variable names or their type:
%macro maxmin;
/* get variable names */
proc contents noprint data = test.hashval out=test.contents;run;
proc sql noprint;
select count(*) into: cnt from test.contents;quit;
%let cnt = &cnt;
proc sql noprint;
select name into: name1 - : name&cnt from test.contents;quit;
/* get length of all variable names and results */
proc delete data = test.results; run;
%let name_len = 0;
%let max_len = 0;
%let min_len = 0;
%do i = 1 %to &cnt;
proc sql noprint;
select max(&&name&i),min(&&name&i) into: max&i, :min&i from test.hashval;quit;
%let max&i = %cmpres(&&max&i);
%let min&i = %cmpres(&&min&i);
%if (&name_len < %length(&&name&i)) %then %let name_len = %length(&&name&i);
%if (&max_len < %length(&&max&i)) %then %let max_len = %length(&&max&i);
%if (&min_len < %length(&&min&i)) %then %let min_len = %length(&&min&i);
%end;
/*create results */
%do i = 1 %to &cnt;
data temp;
length NAME $&name_len MAX $&max_len MIN $&min_len;
NAME = "&&name&i";
MAX = "&&max&i";
MIN = "&&min&i";
run;
proc append base = test.results data= temp force;run;
%end;
%mend maxmin;
%maxmin;
proc sql;
create view myExtrema_1 as
Select min(alphaVar) as alphaVar, ..., put(min(numVar),best32.) as numVar, ...
from myTable
Union
Select max(alphaVar), ..., put(max(numVar),best32.), ...
from myTable;
quit;
proc transpose data=myExtrema_1
out=myExtrema(rename=(
_name_ = Variable
col1 = Minimum
col2 = Maximum
));
var alphaVar ... numVar ...;
run;
On request of the commenter, I tested it with
proc sql;
create view Class_1 as
Select min(Name) as Name
, min(Sex) as Sex
, put(min(Age),best32.) as Age
, put(min(Height),best32.) as Height
, put(min(Weight),best32.) as Weight
from sasHelp.Class
Union
Select max(Name) as Name
, max(Sex) as Sex
, put(max(Age),best32.) as Age
, put(max(Height),best32.) as Height
, put(max(Weight),best32.) as Weight
from sasHelp.Class;
quit;
proc transpose data=Class_1
out=Class(rename=(
_name_ = Variable
col1 = Minimum
col2 = Maximum
));
var Name Sex Age Height Weight;
run;
I have an example like this:
proc sql;
select dealno into :deal_no
from deal_table;
Now I want to traverse the variable deal_no now containing all dealno in table deal_table but I don't know how to do it.
Another option is add 'separated by' to the sql code, which will add a delimiter to the values. You can then use the SCAN function in a data step or %SCAN in a macro to loop through the values and perform whatever task you want. Example below.
proc sql noprint;
select age into :age separated by ','
from sashelp.class;
quit;
%put &age.;
data test;
do i=1 by 1 while(scan("&age.",i) ne '');
age=scan("&age.",i);
output;
end;
drop i;
run;
If you do
%put &deal_no;
you can see that it only contains the first value of dealno, not all of them.
To avoid that you can do something like this:
proc sql;
create table counter as select dealno from deal_table;
select dealno into :deal_no_1 - :deal_no_&sqlobs
from deal_table;
quit;
%let N = &sqlobs;
%macro loop;
%do i = 1 %to &N;
%put &&deal_no_&i;
%end;
%mend;
%loop; run;
Here's another solution.
proc sql noprint;
select age into :ageVals separated by ' '
from ageData;
quit;
%put &ageVals;
%macro loopAgeVals; %let i = 1; %let ageVal = %scan(&ageVals, &i);
%do %while("&ageVal" ~= "");
%put &ageVal;
%let i = %eval(&i + 1);
%let ageVal = %scan(&ageVals, &i);
%end;
%mend;
%loopAgeVals;