DATA Step, Macro, Functions and more

Please Help! Assign Macro from Dataset and Recalling from Memory to write into another Data set

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Please Help! Assign Macro from Dataset and Recalling from Memory to write into another Data set

I can't get the string from memory into my dataset. Instead of going into my Variable, it's coming as a variable by itself! Please help!!

Code which runs by itself

data WORK.TEMP3;

format

  CAT $10.

  AVG_BY_CAT DOLLAR2.

  DATE DDMMYY10.

;

length

  CAT $10.;

INFILE

  DATALINES

  DLM=','

  DSD;

input

  CAT: $10.

  AVG_BY_CAT: DOLLAR2.

  DATE DDMMYY10.

;

DATALINES;

DOGS,1,01/01/2001

CATS,2,02/01/2001

HORSE,3,03/01/2001

FISH,4,04/01/2001

;

run;

%macro JT_MACRO(REF);
%global COUNTER;
%global thisDOLLAR;
%global thisDATE;
%global thisPET;
%let dsid=%sysfunc(open(TEMP3,i));
%let nobs=%sysfunc(attrn(&dsid, nobs));

%do i=1 %to &nobs;
  %let rc=%sysfunc(fetch(&dsid));
  %let COUNTER=%sysfunc(GetVarC(&dsid, %sysfunc(VarNum(&dsid, CAT))));

  %if &COUNTER = &REF %then
   %do;
    %let thisDOLLAR=%sysfunc(GetVarN(&dsid, %sysfunc(VarNum(&dsid, AVG_BY_CAT))));
    %let thisDATE=%sysfunc(GetVarN(&dsid, %sysfunc(VarNum(&dsid, DATE))));
    %let thisPET=%sysfunc(GetVarC(&dsid, %sysfunc(VarNum(&dsid, CAT))));
   %end;
%end;
%let rc = %sysfunc(close(&dsid));
%mend JT_MACRO;
%JT_MACRO(FISH);
%put --> &thisDOLLAR;
%put --> &thisDATE;
%put --> &thisPET;

DATA TEMP4;
SET TEMP3;
format MEM_DOLLAR DOLLAR9. MEM_DATE DDMMYY10. MEM_STR $15.;
length MEM_STR $15.;
MEM_DOLLAR = &thisDOLLAR;
MEM_DATE = &thisDATE;
MEM_STR = &thisPET;
RUN;


Accepted Solutions
Solution
‎05-09-2013 09:32 PM
New Contributor
Posts: 4

Re: Please Help! Assign Macro from Dataset and Recalling from Memory to write into another Data set

Hi Tsengj,

In the last datastep, assign quotation for the MEM_STR Varaible

DATA TEMP4;
SET TEMP3;
format MEM_DOLLAR DOLLAR9. MEM_DATE DDMMYY10. MEM_STR $15.;
length MEM_STR $15.;
MEM_DOLLAR = &thisDOLLAR;
MEM_DATE = &thisDATE;
MEM_STR = "&thisPET";
RUN;

View solution in original post


All Replies
Super User
Posts: 5,503

Re: Please Help! Assign Macro from Dataset and Recalling from Memory to write into another Data set

I don't see where you assign a value to &REF.  Is it possible that the %IF &COUNTER=&REF condition is never true?

Also note, if you are trying to get the last observation from the data set, there are faster ways to do it.  In fact, from the application you are writing, it seems like any observation would do. Shouldn't VarNum return the same value for every observation?

Good luck.

Solution
‎05-09-2013 09:32 PM
New Contributor
Posts: 4

Re: Please Help! Assign Macro from Dataset and Recalling from Memory to write into another Data set

Hi Tsengj,

In the last datastep, assign quotation for the MEM_STR Varaible

DATA TEMP4;
SET TEMP3;
format MEM_DOLLAR DOLLAR9. MEM_DATE DDMMYY10. MEM_STR $15.;
length MEM_STR $15.;
MEM_DOLLAR = &thisDOLLAR;
MEM_DATE = &thisDATE;
MEM_STR = "&thisPET";
RUN;

Occasional Contributor
Posts: 7

Re: Please Help! Assign Macro from Dataset and Recalling from Memory to write into another Data set

Posted in reply to siliveri4uall

Bingo! Thanks.

Super User
Super User
Posts: 7,042

Re: Please Help! Assign Macro from Dataset and Recalling from Memory to write into another Data set

Before you begin attempting to write macro code you need to understand what SAS code you want the macro logic to generate.  For your issue the problem is that to assign a text literal to character variable in a data step you need to enclose the value in quotes.  If you want macro references in the text string to be expanded then use double quotes.

MEM_STR = "&thisPET" ;

Normally you would not want use macro variables to move values from  one dataset to  another as SAS has many language features (SET, MERGE, PROC SQL) to allow you to do that without first converting the values into character strings in macro variables.

Are you just playing around to understand macro coding or do you have an actual problem that you are trying to solve?  If you can explain it then perhaps there is an easier way to do what you need.

Occasional Contributor
Posts: 7

Re: Please Help! Assign Macro from Dataset and Recalling from Memory to write into another Data set

Just playing around but i know i'll need to reference Macro generated when i created data sets. Thanks!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 200 views
  • 3 likes
  • 4 in conversation