BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
xliu1
Quartz | Level 8

Attached is the macro definition. The programs runs and returns the expected results. But I get warning in the SAS log. The college list is supposed to be: A&S,AICC,BCS,DRP,EDU,ENG,HLTH,MBUS,NUR,OUS. When I call the macro, I get warning in the SAS log: How can I fix the code to remove the warning? Thanks.

xliu1_0-1782404601279.png

 

%macro fit_autoreg_params;

  data cohort_params_all;
    length college $32 parameter $32 estimate 8;
    stop;
  run;

  data selected_vars_all;
    length college $32 variable $32 model_status $20;
    stop;
  run;

  %let n=%sysfunc(countw(%superq(college_list), %str(,)));

  %do i=1 %to &n;

    %let this_college=%scan(%superq(college_list), &i, %str(,));

    data hist_one;
      set cohort_hist;
	  where college=%tslit(&this_college);
      *where college = "&this_college";
      time_c = cohort_yr - &mean_year;
    run;

    data hist_one_recent;
      set hist_one;

      if college eq "OUS" and cohort_yr <= 2006 then delete;
      if college eq "NUR" and cohort_yr = 2006 then delete;
    run;

    proc sql noprint;
      select count(*) into :nobs trimmed
      from hist_one_recent;
    quit;

    %if &nobs >= 6 %then %do;

      ods exclude all;
      ods output ParameterEstimates=pe_one;

      proc autoreg data=hist_one_recent;
        model COHORT_6YR =
              time_c female_perc sch100_perc gpa2_perc senior_perc
              / nlag=1 method=ml backstep dw=5 dwprob;
      run;
      quit;

      ods select all;

      data pe_one2;
        length college $32 parameter $32 estimate 8;
        set pe_one(rename=(Variable=_Variable Estimate=_Estimate));
        
		college=%tslit(&this_college);
        *college = "&this_college";
        parameter    = strip(_Variable);
        estimate     = _Estimate;

        if upcase(parameter) = "AR1" then delete;

        keep college parameter estimate;
      run;

      proc append base=cohort_params_all data=pe_one2 force;
      run;

      data selected_one;
        length college $32 variable $32 model_status $20;
        set pe_one(rename=(Variable=_Variable));

		college=%tslit(&this_college);
        *college = "&this_college";
        variable     = strip(_Variable);
        model_status = "backstep_kept";

        if upcase(variable) = "AR1" then delete;

        keep college variable model_status;
      run;

      proc append base=selected_vars_all data=selected_one force;
      run;

    %end;

    %else %if &nobs >= 2 %then %do;

      /* Trend fallback: COHORT_6YR = Intercept + time_c */

      proc sort data=hist_one_recent;
        by cohort_yr;
      run;

      ods exclude all;
      ods output ParameterEstimates=pe_fallback;

      proc reg data=hist_one_recent;
        model COHORT_6YR = time_c;
      run;
      quit;

      ods select all;

      data pe_one2;
        length college $32 parameter $32 estimate 8;
        set pe_fallback(rename=(Variable=parameter Estimate=estimate));

		college=%tslit(&this_college);
        *college = "&this_college";

        keep college parameter estimate;
      run;

      proc append base=cohort_params_all data=pe_one2 force;
      run;

      data selected_one;
        length college $32 variable $32 model_status $20;
        college=%tslit(&this_college);
		*college = "&this_college";

        variable = "Intercept"; model_status = "fallback_trend"; output;
        variable = "time_c";    model_status = "fallback_trend"; output;
      run;

      proc append base=selected_vars_all data=selected_one force;
      run;

    %end;

    %else %do;

      /* Very sparse fallback: flat last observed value */

      proc sort data=hist_one_recent;
        by cohort_yr;
      run;

      data _null_;
        set hist_one_recent end=lastobs;
        if lastobs then call symputx('last_cohort', COHORT_6YR);
      run;

      data pe_one2;
        length college $32 parameter $32 estimate 8;
        college=%tslit(&this_college);
        *college = "&this_college";

        parameter = "Intercept"; estimate = &last_cohort; output;
        parameter = "time_c";    estimate = 0;            output;
      run;

      proc append base=cohort_params_all data=pe_one2 force;
      run;

      data selected_one;
        length college $32 variable $32 model_status $20;
        college=%tslit(&this_college);
        *college = "&this_college";

        variable = "Intercept"; model_status = "fallback_flat"; output;
        variable = "time_c";    model_status = "fallback_flat"; output;
      run;

      proc append base=selected_vars_all data=selected_one force;
      run;

    %end;

  %end;

%mend;

  

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @xliu1,

 

Using %QSCAN instead of %SCAN should resolve the issue, as it masks the ampersand (macro trigger) in "A&S".

%let this_college=%qscan(%superq(college_list), &i, %str(,));

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hello @xliu1,

 

Using %QSCAN instead of %SCAN should resolve the issue, as it masks the ampersand (macro trigger) in "A&S".

%let this_college=%qscan(%superq(college_list), &i, %str(,));
xliu1
Quartz | Level 8

Thank you! It works out in the program and removed SAS warning.

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand in the Innovate Hub.

Watch 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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 227 views
  • 3 likes
  • 2 in conversation