I am trying to compute the same variables across an library of datasets. The problem is, the month and year vars are not included in the files. The code below interpreds year and month from the file name and is supposed to insert yy for year, and mm for month into the file as new variables. However, the code listed below is inserting year and month as the year and month of the most recent file read on the last dataset. Any suggestions?
%Macro Libdata;
libname rtot 'H:/...../.../RTOT';
libname test 'H:/...../.../Test';
proc contents data=rtot._all_ memtype=data out=out noprint;
run;
proc sort data=out;
by memname name; run;
data a;
set out;
by memname name;
if first.memname;
run;
data _null_;
set a end=last;
by memname name;
call symput('DS'|| left(_n_),trim(memname));
call symput('yy',substr(memname,10,2)); /*Var Year is not found in summary data*/
call symput('mm',substr(memname,12,2)); /*Month is not found in summary data*/
if last then call symput('TOTAL',left(_n_));
run;
%do i=1 %to &total;
data test.&&DS&i;
set rtot.&&DS&i;
if count gt 0 then resp_rate=round(usable/count*100,1);
cv=round(cv,.01);
exp_tot=round(exp_tot,1);
MON="&mm"; /*insert month into file*/
Yr="&yy"; /*insert year into file*/
Crop=substr(Varname1,2,3);
run;
%end;
%mend Libdata;
Credits to: *http://www2.sas.com/proceedings/sugi27/p084-27.pdf;
This fixed it.... needed a double &&mm&i.
MON="&&mm&i"; /*insert month into file*/
Yr="&&yy&i"; /*insert year into file*/
data _null_; set a end=last; by memname name; call symput('DS'|| left(_n_),trim(memname)); call symput('yy',substr(memname,10,2)); /*Var Year is not found in summary data*/ call symput('mm',substr(memname,12,2)); /*Month is not found in summary data*/ if last then call symput('TOTAL',left(_n_)); run;
Here's your problem. Each time the data set executes the current observation, it assigns a value to &yy and to &mm, and when it executes the last observation, this value for &yy and &mm is all that is available in future steps of your code.
If you want each observation of the data set to have it's own &yy and its own &mm then you probably need to have something like
call symput('yy'||left(_n_),substr(memname,10,2));
and similarly adjust the calls to &yy later on in the code.
@jakestat wrote:
Page, the computations work such that the other vars are updated.
Could you please be specific about what you mean? What other variables are updated?
I don't see the relationship of resp_rate to your original question. As far as I can tell from looking at your code, I have provided an answer to your original question.
This fixed it.... needed a double &&mm&i.
MON="&&mm&i"; /*insert month into file*/
Yr="&&yy&i"; /*insert year into file*/
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.