BookmarkSubscribeRSS Feed
Jennifer_Atlas
Calcite | Level 5
Hi,

I am trying to extract a value from a variable and set it as a global constant that I can call in a different data set. What I have right now is resolving to the variable names and not the constant values that I need. I need to be able to use this value in a do loop.

What I have done:

1) I have extracted the minimum and maximum values for a variable in proc means and created a data set for each of these values.

2) I need to use these values in a new data set that looks somewhat like

data blah.blah;
set different.data;

do i = MIN to MAX;

etc;

I need to be able to pull those min and max values out as constants so that I can use them in this new dataset. Does anyone know how I can do this?


Thanks!
Jennifer
6 REPLIES 6
DF
Fluorite | Level 6 DF
Fluorite | Level 6
One option would be to use a SAS Macro variable to store your values. This would all you to extract your values in one section of your code, store the min/max values in macros, and then refer to those later in your code.

The symbol generator would replace references to the macros in your code with their saved values, after which your code would be compiled and ran.

There's some simple examples on the help pages http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001071889.htm.

Hope that helps!
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
If you are wanting to capture a SAS variable value and make it available in another DATA step (or somewhere in your current SAS session), you will need to look at using CALL SYMPUT. Then, with the SAS global macro variable assigned, you can reference that macro variable elsewhere in your current SAS session. It is important to learn and understand how SAS macro variables are accessed, assigned, and referenced in the different parts of the SAS system, particularly SAS macro based programs, SAS DATA step variables (through an assignment or RETAIN statement), and as a constant/literal value in a SAS language expression / statement.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search arguments, this topic / post:

macro variable introduction site:sas.com

macro variable scope symput site:sas.com

resolving macro variables in code site:sas.com

macro variable debugging site:sas.com

macro variable symbol resolution site:sas.com
Jennifer_Atlas
Calcite | Level 5
Thanks for your help. I've been trying to get SYMPUT to work but the language is a little confusing. Here's what finally worked:


/* GET MIN & MAX */
proc means data=gds1 noprint;
output out=fix1;
run;

/* PULL OUT MIN*/

data fix2a;
set fix1;
if _STAT_ = 'MIN' then bot0 = &var1;
if bot0 = . then delete;
CALL SYMPUT('bot1', PUT(bot0, 3.));
keep bot0;
run;

%put &bot1;


/* PULL OUT MAX*/
data fix2b;
set fix1;
if _STAT_ = 'MAX' then top0 = &var1;
if top0 = . then delete;

CALL SYMPUT('top1', PUT(top0, 3.));
keep top0;
run;

%put &top1;


/* I'm sure there are some redundancies in this but I was trying to break it down to each little step to figure out what was going wrong. */
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Suggest you share your SAS log output with all code revealed for useful feedback - also you have a macro var reference &var1 -- we don't see that variable assigned anywhere? Is this PROC/DATA step code called from within a macro - or how/where is macro variable VAR1 assigned? Really need to see your log - also tell more about your outward symptom, if unexpected (and what's expected too).

And, for desk-checking your flow, here's a SAS statement to turn on diagnostics:

OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN MPRINT /* MLOGIC */ ;

Also, for DATA step analysis / diagnostics, code this statement at various points (must be at SAS 9 to use PUTLOG):

PUTLOG / _all_;

Scott Barry
SBBWorks, Inc.
darrylovia
Quartz | Level 8
PROC SQL can also create macro variables. I suggest that you peruse the PROC SQL documentation for details. Below is a rough code example;


proc sql;
select min(bot0) as min , max(bot0 ) as max
from gds1;
quit;

data fix2b;
set fix1;

do i=&min to &max;
endd;

run;

I would verify that the min and max are integers

Regards
Darryl
chang_y_chung_hotmail_com
Obsidian | Level 7
@Jennifer:

I am not sure if you need to pass you statistics to macro variables to be used in another data step. In general, it is better to use little macros; and to avoid the global macro variables altogether. There are inevitable losses of precision involved when you convert numeric values to characters and back. Not using global macro vars, thus, your results can be accurate. In addition, the code gets (much) simpler without global macro vars in many cases. For instance, you can do something like below:



   proc summary data=sashelp.class;

     var age;

     output out=stat(drop=_:) min(age)=minAge max(age)=maxAge;

   run;

 

   data _null_;

     if _n_ = 1 then set stat;

     do age = minAge to maxAge;

       put age=;

     end;

     stop;

   run;

   /* on log

   age=11

   age=12

   age=13

   age=14

   age=15

   age=16

   */

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 8825 views
  • 0 likes
  • 5 in conversation