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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 755 views
  • 1 like
  • 3 in conversation