BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
leonzheng
Obsidian | Level 7

I met problem using macro variable in my SAS code.

I need to define a macro variable and keep the column in dataset operation.

%let target_parm = temperature; // so I can change temperature into other parameter later if necessary

.

.

//pull data and form a dataset called rawdata which includes a column called temerature

data output (keep = day month year &target_parm.);

 set rawdata;

run;

 

This will return error variable name is not valid.

I saw examples online of referring macro variable usually is &target_parm without ".", but I need to use the form with "." otherwise in some other parts this macro variable will not work (like the operation of pulling temperature data). 

Can someone explain to me the difference of w/o "." in &target_parm? And how should I modify the code so the macro variable can be used in keep function.

 

Thanks,

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

PS your basic idea for the algorithm is sound, see this simple example:

data class;
set sashelp.class (keep=name sex);
value = 1;
run;

%let parm=F;

proc transpose data=class out=class1;
by name;
var value;
id sex;
run;

data want;
set class1 (keep=name &parm.);
run;

so it must be something that either prevents the setting of &PARM in the necessary context (eg local vs. global symbol table), or you have some code that clears &parm from the symbol table.

View solution in original post

11 REPLIES 11
novinosrin
Tourmaline | Level 20

You wrote -" This will return error variable name is not valid"

 

Why you say that?

 

It seems to work fine in the below example

 

%let target_parm = height;

data output (keep = name &target_parm.);

set sashelp.class;

run;

leonzheng
Obsidian | Level 7
It is not working on my side.
ERROR: variable name & is not valid.
ERROR: Invalid for the KEEP option.
novinosrin
Tourmaline | Level 20

Please post your code and log

leonzheng
Obsidian | Level 7

/************************ define variable *****************************/
%let target_wafer = 288760;
%let target_op = 9689;
%let target_parm = 'HEATERSUB';
%let PARM = HEATERSUB;


/************************ pull raw data from CHIMP *****************************/
proc sql;
connect to db2 (&wafer_server.);
create table rawdata
as select * from connection to db2 (

SELECT A.WAFERNUM, A.PROCESSDATE, A.HT, A.OP, A.WAFERSIZE, A.PARMNAME, A.ROWNUM, A.COL01,A.COL02,A.COL03,A.COL04,A.COL05,A.COL06,A.COL07,A.COL08,
A.COL09,A.COL10,A.COL11,A.COL12,A.COL13,A.COL14,A.COL15,A.COL16,A.COL17,A.COL18,A.COL19,A.COL20,A.COL21,A.COL22,
A.COL23,A.COL24,A.COL25,A.COL26,A.COL27,A.COL28,A.COL29,A.COL30,A.COL31,A.COL32,A.COL33,A.COL34,A.COL35,A.COL36,
A.COL37,A.COL38,A.COL39,A.COL40,A.COL41,A.COL42,A.COL43,A.COL44,A.COL45,A.COL46,A.COL47,A.COL48,A.COL49,A.COL50,
A.COL51,A.COL52,A.COL53,A.COL54,A.COL55,A.COL56,A.COL57,A.COL58,A.COL59,A.COL60
From wtooldet.CHIMP A
WHERE A.WAFERNUM in (&target_wafer.)
AND A.PARMNAME in (&target_parm.)
AND A.OP in (&target_op.)

order by ROWNUM

);

disconnect from db2;
quit;

 


proc transpose data=rawdata
out=rawdata_transp
name=COLNUM;
label COLNUM='COLNUM';
by WAFERNUM WAFERSIZE ROWNUM PROCESSDATE;
var COL01 - COL60;
id PARMNAME;
run;

 

data rawdata_transp (keep = WAFERNUM WAFERSIZE ROWNUM COLNUM &PARM.);
set rawdata_transp;
colnum = substr(COLNUM,4,2);
run;

 

 

ERROR:

1303 data rawdata_transp (keep = WAFERNUM WAFERSIZE ROWNUM COLNUM &PARM.);
-
214
23
ERROR 214-322: Variable name & is not valid.

ERROR 23-7: Invalid value for the KEEP option.

-----
23
1303! data rawdata_transp (keep = WAFERNUM WAFERSIZE ROWNUM COLNUM &PARM.);
-----
214
WARNING: Apparent symbolic reference PARM not resolved.
ERROR 214-322: Variable name PARM. is not valid.

1304 set rawdata_transp;
1305 colnum = substr(COLNUM,4,2);
1306 run;

 

I defined two macro variable to define HEATERSUB w/o '', because the '&PARM.' is not working.

novinosrin
Tourmaline | Level 20

That's strange.  But can  you please run a proc contents to check whether or not there is a variable by the name HEATERSUB in your dataset rawdata_transp?

leonzheng
Obsidian | Level 7
Yes there is
Kurt_Bremser
Super User

This is the crucial message:

WARNING: Apparent symbolic reference PARM not resolved.

So it seems that your

%let PARM = HEATERSUB;

somehow does not work. I have an inkling you are not showing us all your code.

Add

%put _global_;

at appropriate locations in your code to reveal the status of your global macro symbol table.

Kurt_Bremser
Super User

PS your basic idea for the algorithm is sound, see this simple example:

data class;
set sashelp.class (keep=name sex);
value = 1;
run;

%let parm=F;

proc transpose data=class out=class1;
by name;
var value;
id sex;
run;

data want;
set class1 (keep=name &parm.);
run;

so it must be something that either prevents the setting of &PARM in the necessary context (eg local vs. global symbol table), or you have some code that clears &parm from the symbol table.

leonzheng
Obsidian | Level 7
thx, I've figure it out, there was something else wrong
leonzheng
Obsidian | Level 7
The reason is part of my code works in server environment and part is in desktop environment. The macro variable defined on server is not recognized by my local machine.

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
  • 11 replies
  • 4285 views
  • 3 likes
  • 3 in conversation