Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic help regarding MACROS

Status
Not open for further replies.

sarav1981

Programmer
Jul 14, 2005
29
US
Hi all,

I am a rookie programmer in SAS. I was trying to learn SAS Macros and I got myself into a real mess in a basic step. I need to store a variable list to a macro variable and then export the variable list in another variable.

I have a data set flights.diabetes which has 20 records in a character variable 'ID'. I am trying to store the entire 20 records into 'varlist'. Then I would like to create a new dataset 'new' and would like to transfer all the 20 records to a new variable 'wt' in the new dataset.

I tried the following code:

data _null_;
set flights.diabetes;
allid = left(id);
call symput('varlist', allid);
run;

%put &varlist;

data new;
wt = &varlist;
run;

All I get in 'wt' is the last value of ID. What should I do to get the entire list of values. Should I try to use arrays to store the entire list?

It would be great if you guys can help me on this as I presume simple MACRO concept.

Thanks,
Sarav
 
The reason you get the last record is you are overwriting your macro var with each new observation (record)that the data step goes through. Remember that a data step is an implicit loop. Also, the call symput function is not executed until the 'run' statement is hit.

You need to generate a list of id's and insert it into a string. Then, at the last record insert that string into a macro var. You don't need to call the 'call symout' function at every observation. Here is an example of what I describe.

Code:
data _null_;
  set flights.diabetes end =last;
  retain z m;
  length allid m $500;
  z= x;
  if _n_ eq 1 then
    allid =  trim(z);
  else 
    allid = trim(m) ||' '||trim(z);
  m = allid;
  if last then
    call symput('varlist',allid);
run;

I hope that this helps you.
Klaz
 
Hi Klaz2002,

Thanks a lot for your help. Now I see the logic clearly.

Sarav
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top