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!

Symput Null Error - not picking up text

Status
Not open for further replies.

gordonme

Programmer
Dec 1, 2009
2
US
Hello,
I am reading in an Access table with reason codes and then assigning them to global SAS variables. They are all stored as type text, but the issue I'm running into is that they seem to be randomly working.

data temp; set reason;
call symput('Week',weekReason);
call symput('Wslr',WslrRule);
call symput('St',StRule);

if _n_ = 8 then output;
%put _Global_
run;

Then I see that week and wslr have a value assigned while &St is null. If I look at data temp, StRule is UT and should not be null in the dataset. Also if I manually pass in a string with single quotes it does not work, but if I use double quotes it does. For example:
call symput('St',"UT"); <- works
call symput('St','UT'); <- does not work

Does anyone have any ideas why this is happening, or any suggestions for a workaround? This is driving me insane and I need to get it worked out.

Thank you!
 
The symput function creates macros but they are not available until after the 'RUN' statement. try moving the %global statement and place it after the RUN statement.

Also the dataset 'reason' may have more than 1 row. Remember that SAS implictly loops through each row of your data. What I mean to point out is the Symput is resetting the particular macro var on each observation. So what you probably see are the values on the last record of that dataset/table.

To get them all you can create a list macro vars using the _n_ var.
Code:
data temp; 
   set reason end=last;
   call symput('Week'||trim(left(put(_n_,8.))),trim(weekReason));
   call symput('Wslr'||trim(left(put(_n_,8.))),trim(WslrRule));  
   call symput('St'||trim(left(put(_n_,8.))),trim(StRule)); 
      if last then 
       call symput('limit',trim(left(put(_n_,8.))) );
run;
%put _Global_;

If the values that you're interested in reside in the 8th observation then you can do the following:

Code:
data temp; 
   set reason;
   if _n_=8 then do;
     call symput('Week',trim(weekReason));
     call symput('Wslr',trim(WslrRule));  
     call symput('St',trim(StRule)); 
   end;
run;
%put _Global_;

Hope this helps.
Klaz
 
Klaz already addressed the first part of your question.

Regarding the inconsistency that you observed with the macro variable quoting, I beleive this has nothing to do with the quoting, and is simply due to the placement of the global statement in your code (you are seeing the variable created in the previous submit of code).

Just be sure this is not the case, you should either delete or reset the variables at the start of your code. In SAS9.2, you can use the nowarn option to suppress warning messages raised when trying to delete macro variables that don't exist.

A 'good practice' when creating macro variables from datasets via symput or sql, is to set the macro variable to empty at the start of your code (via %let x=;), to prevent the warning message 'WARNING: Apparent symbolic reference STA not resolved.' when you have an empty dataset.

Code:
%symdel  STA STB / nowarn ;
data _null_ ;
   call symput('STA','UT'); 
   call symput('STB',"UT"); 
   stop ;
   run ; 
%put STA=&STA STB=&STB ;

HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top