简体   繁体   中英

resolve macro variables for saving/naming a dataset in SAS

I have problem saving a dataset using macro variables to a desired directory.

Basically, I want to save the dataset "_est" to library "sret" according to the values of &var and &age. I wrote the following code:

%let var=k;
%let age=2;
...
...
data sret.est_&var&age._b3; 
  set _est;
run;

What I want is a dataset named as "est_k2_b3.sas7bdat" in "sret". But what the code gives me is a dataset "est_k2.sas7bdat" saved in the folder I want and another dataset "_b3" in the working library. Both datasets are identical. I'm quite puzzled how to solve this.

As itzy pointed out you have a space after "2" that splits your dataset name in two.

I can replicate the issue only defining the macro variable age with a call symput:

data _null_;
    age='2 ';
    call symput('age',age);
run;

If this is the case you can solve it by removing the space in the data step with a strip() , using a call symputx() (to be used with numbers) or re-declaring your variable after the data step with a %let , that automatically removes spaces:

%let age= &age.;

Had a very similar problem. Somehow a space is added until you use strip(). Here's the example below.

data test;
    input numdays;
    datalines;
    31
;

%macro monthly(months);

%let count=%sysfunc(countw(&months.));
%do i=1 %to &count.;
    %let value=%qscan(&months.,&i,%str(,));
    %let month=%sysfunc(strip(&value.));
    %put &value.;
    %put &month.;

data value_&value.;
    set test;
run;

data month_&month.;
    set test;
run;

%end;
%mend;
%monthly(%str(oct,jan));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM