BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
maroulator
Obsidian | Level 7

I am applying the following code to the attached .csv file; I am trying to assign each of the observations corresponding to dataset StepRateSchedules to macro variables and then have each of the macro variables displayed in my log (%put statements). For some reason, my macro references below do not get resolved.

Any ideas on why that may be the case and how I can accomplish my goal?


data WORK.StepRateSchedules;
     infile '/FilePath/RateSchedule.csv' dsd lrecl=40000 firstobs=3;
     informat Cusip $10.;
     informat AnnualCouponRateDates mmddyy10.;
     informat AnnualCouponRates 5.;
     format AnnualCouponRateDates yymmddn8.;
     input Cusip AnnualCouponRateDates AnnualCouponRates;
run;


%macro RateAssignment();

  %let dsid=%sysfunc(open(StepRateSchedules));
  %let Nobs=%sysfunc(attrn(&dsid,nlobs));
  %let dsid=%sysfunc(close(&dsid));

  %do i=1 %to &Nobs;
    proc sql;
      select AnnualCouponRates, AnnualCouponRateDates
      into :Rate&i, :RateDate&i
      from StepRateSchedules (obs=&i);
  %end;


%mend;

%RateAssignment();

%put &&Rate&i;

%put &&RateDate&i;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Those macro variables are created locally so disappear when the macro ends.

Don't use a macro for this though, SQL will create a list of variables for you. Choose an endpoint that will always be greater than the number of observations and SAS will only create the macro variables needed.

proc sql;
  select AnnualCouponRates, AnnualCouponRateDates
  into :Rate1-:Rate99999, :RateDate1-:RateDate99999
  from StepRateSchedules;

quit;

%put &rate1;

%put &rate99;

%put &rate1000;

View solution in original post

4 REPLIES 4
Reeza
Super User

Those macro variables are created locally so disappear when the macro ends.

Don't use a macro for this though, SQL will create a list of variables for you. Choose an endpoint that will always be greater than the number of observations and SAS will only create the macro variables needed.

proc sql;
  select AnnualCouponRates, AnnualCouponRateDates
  into :Rate1-:Rate99999, :RateDate1-:RateDate99999
  from StepRateSchedules;

quit;

%put &rate1;

%put &rate99;

%put &rate1000;

maroulator
Obsidian | Level 7

Reeza,

Thanks, this is a very good suggestion; for some reason I tend to default to macros. One follow up question on the above, however.

I need to be able to list out ALL variables defined in my list (ie from &Rate1 to &&Rate&SQLOBS) and I won't necessarily know how many are in each list. Any simple suggestions?

Reeza
Super User

You can still capture the &sqlobs and use that.

But why the need to list them out? If you have to display them then use the display from teh SQL table

or look at sashelp.vmacro table

Astounding
PROC Star

The quick and dirty way is easy to program:

%put _user_;

However, it may give you a little more information than you are looking for, and in a different order than you are hoping for.

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
  • 4 replies
  • 1531 views
  • 1 like
  • 3 in conversation