BookmarkSubscribeRSS Feed
bayoote
Calcite | Level 5

Hi All,

 

I have a large dataset and I want to do the interpolation for each group based on four variables. I have split the datasets into multiple datasets. Each dataset is a group that I will do the interpolation. However, I still need to get the number of observations for each dataset and save them as macro variables in order to do the interpolation.

 

Attached is my SAS code. My macro variable macvar&i is not resolved and don't know is there anything wrong with my code.  Anybody can help with that? Thanks!!!

 

 

 

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

Do not post MS Office documents.

Post code using the running man or the {i} icons.

 

One possible way (can't test atm):

data _null_;
  set A nobs=NOBSA;
  call symputx('NOBSA',NOBSA);
  set B nobs=NOBSB;
  call symputx('NOBSB',NOBSB);
  stop;
run;

 

 

 

 

PaigeMiller
Diamond | Level 26

@bayoote wrote:

Hi All,

 

I have a large dataset and I want to do the interpolation for each group based on four variables. I have split the datasets into multiple datasets. Each dataset is a group that I will do the interpolation. However, I still need to get the number of observations for each dataset and save them as macro variables in order to do the interpolation.

 

Attached is my SAS code. My macro variable macvar&i is not resolved and don't know is there anything wrong with my code.  Anybody can help with that? Thanks!!!

 


You'd be better off keeping these in one SAS data set and then doing the analysis in SAS using a BY statement, and then you don't need to determine the number of observations in each group.

 

By the way, if everything is in one SAS data set, it is extremely simple to determine the number of observations in each group, all at once, no need to loop or repeat code four times. You can do this in PROC MEANS or PROC SUMMARY or PROC FREQ.

--
Paige Miller
Patrick
Opal | Level 21

@bayoote wrote:

Hi All,

 

I have a large dataset and I want to do the interpolation for each group based on four variables. I have split the datasets into multiple datasets. Each dataset is a group that I will do the interpolation. However, I still need to get the number of observations for each dataset and save them as macro variables in order to do the interpolation.

As others already wrote it's likely much better to keep everything in a single data set and use By Group processing. One could call this best practice.

It also helps if you don't only post your code but also some sample data and especially the error/warning you get in the log.

 

Here your code amended with sample data and issues fixed.

/* create sample data */
data have;
  do group=1 to 2;
    do product='a';
      do test=1 to 1;
        do condition='x';
          do x=1 to 5;
            ign=1;
            y=ceil(ranuni(1)*10);
            output;
          end;
        end;
      end;
    end;
  end;
  stop;
run;

/*split my dataset to multiple datasets based on the four variables*/
data count;
  set have end=end;
  by group product test condition;
  if first.condition then
    do;
      count+1;
      call symputx(cats('groupcnt',count),count);
    end;
  if end then
    call symputx('numgroups',count);
run;

%macro createit;
  %do i=1 %to &numgroups;
    data new_&&groupcnt&i;
      set count;
      where count=&&groupcnt&i;
    run;
  %end;
%mend createit;

/* get the number of rows in each dataset and save them as macro variables*/
%macro get_obs;
  %do i=1 %to &numgroups;
    data _null_;
      set Rong.new_&&groupcnt&i NOBS=size&i;
      call symputx("macvar&i",size&i,'g');
      stop;
    run;
  %end;
%mend;

/* do interpolation for each dataset*/
%macro interpolation;
  %do i=1 %to &numgroups;
    proc sort data=new_&&groupcnt&i;
      by IGN;
    run;
    proc transreg data=new_&&groupcnt&i;
      model identity(Y)=spline(X /degree=1 nknots=%eval(&&macvar&i-2));
      output out=prediction&i  predicted;
    run;
  %end;
%mend interpolation;

/* call the macros */
options mprint;
%createit;
libname rong (work);
%get_obs;
%interpolation;

 

 

The issues were (already fixed in above code):

1. scope of macro variable

WARNING: Apparent symbolic reference MACVAR1 not resolved.

You get this warning because macro variables MACVAR<n> get created in macro %get_obs with a local scope. They only exist with the macro and you can't use them in macro %interpolation.

To create the variables with a global scope use the 'g' option in the call symputx() function.

 

2. Calculations on macro level require %EVAL() or %SYSEVALF() function.

98       ! identity(Y)=spline(X /degree=1 nknots=&&macvar&i-2);       output out=prediction&i  predicted;     run;
                                                           __
                                                           22
ERROR 22-322: Syntax error, expecting one of the following: ), AFTER, AIC, AICC, ALPHA, CENTER, CLL, CONVENIENT, CPREFIX, CV, 
              DEGREE, EFFECTS, EVENLY, EXKNOTS, GCV, GEOMETRICMEAN, KNOTS, LAMBDAS, LPREFIX, NKNOTS, ORDER, ORIGINAL, ORTHOGONAL, 
              PARAMETER, RANGE, REFLECT, SBC, SEPARATORS, SM, STANDORTH, TSTANDARD, Z, ZERO.  

 

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1680 views
  • 3 likes
  • 4 in conversation