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;
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;
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.
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;
Bingo! Thanks.
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.
Just playing around but i know i'll need to reference Macro generated when i created data sets. Thanks!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.