SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
frupaul
Quartz | Level 8

Hi everyone,

 

I have the following code:

%let test_filename = JJJ_CP_LUKAKU;
%let T_word2 = %scan(&test_filename,2,_);
%let Agreed_Word2 = CLB;

data _null_;
   File print;
   If &T_word2 ^= &Agreed_Word2 then put/ "Failed: Second word of Filename is " &T_word2 
										". Should be &Agreed_Word2.";
   else put /"File is fine";
run;

 

 

The objective to check the file that is supplied to see if it is names accordingly. So the second word of the file should be CLB. If it doesn't match this, a message should be displayed saying it doesn't match, otherwise a message is displayed saying the file is fine. In the case above, it clearly doesn't match and a message is issued saying it does match (as below).

 

Does anyone know how to resolve this please?

 

Capture4.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

 

%let test_filename = JJJ_CP_LUKAKU;
%let T_word2 = %scan(&test_filename,2,_);
%let Agreed_Word2 = CLB;

data _null_;
   File print;
   If &T_word2 ^= &Agreed_Word2 then put/ "Failed: Second word of Filename is " &T_word2 
										". Should be &Agreed_Word2.";
   else put /"File is fine";
run;

 

Maxim 2: read the log!

When the macro variables are resolved, this code results:

 

data _null_;
   File print;
   If CP ^= CLB then put/ "Failed: Second word of Filename is " CP 
										". Should be CLB.";
   else put /"File is fine";
run;

As you can see, the data step tries to compare variable CP with variable CLB. You will get a message that variables CP and CLB are uninitialized.

Run this instead to compare strings:

data _null_;
   File print;
   If "&T_word2." ^= "&Agreed_Word2."
   then put/ "Failed: Second word of Filename is &T_word2.. Should be &Agreed_Word2..";
   else put /"File is fine";
run;

 

Edit: corrected last program example to display proper string in then branch.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

 

%let test_filename = JJJ_CP_LUKAKU;
%let T_word2 = %scan(&test_filename,2,_);
%let Agreed_Word2 = CLB;

data _null_;
   File print;
   If &T_word2 ^= &Agreed_Word2 then put/ "Failed: Second word of Filename is " &T_word2 
										". Should be &Agreed_Word2.";
   else put /"File is fine";
run;

 

Maxim 2: read the log!

When the macro variables are resolved, this code results:

 

data _null_;
   File print;
   If CP ^= CLB then put/ "Failed: Second word of Filename is " CP 
										". Should be CLB.";
   else put /"File is fine";
run;

As you can see, the data step tries to compare variable CP with variable CLB. You will get a message that variables CP and CLB are uninitialized.

Run this instead to compare strings:

data _null_;
   File print;
   If "&T_word2." ^= "&Agreed_Word2."
   then put/ "Failed: Second word of Filename is &T_word2.. Should be &Agreed_Word2..";
   else put /"File is fine";
run;

 

Edit: corrected last program example to display proper string in then branch.

andreas_lds
Jade | Level 19

The log should contain the following notes:

NOTE: Variable CP is uninitialized.
NOTE: Variable CLB is uninitialized.

As first line of your code insert

options mprint symbolgen mlogic;

Run the program and see what the macro compiler created.

 

The error is quite obvious.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

You are mixing up macro and datastep.  Macro is nothing more than a find replace system.  So lets replace the macro part with the actual values:

data _null_;
   If CP ^= CLB then put/ "Failed: Second word of Filename is " CP 
                              ". Should be CLB";
   else put /"File is fine";
run;

Now you can clearly see why your code does not work.  There is no CP variable, nor CLB variable hence nothing=nothing, so the file is fine.  If you put quotes around each part then it should work;

%let test_filename=JJJ_CP_LUKAKU;
%let T_word2 = %scan(&test_filename,2,_);
%let Agreed_Word2 = CLB;

data _null_;
   If "&T_word2" ^= "&Agreed_Word2" then put/ "Failed: Second word of Filename is &T_word2. Should be &Agreed_Word2.";
   else put /"File is fine";
run;

That being said, its really not a good idea in any sense to be doing data processing in macro, as shown above it is just a find/replace system, it does nothing itself just generates some text.  A better way of writing the same thing would be:

%let test_filename=JJJ_CP_LUKAKU;
%let agreed_word2=CLB;

data _null_;
  if scan("&test_filename.",2,"_") ne "&agreed_word2." then put "Failed: Second word of Filename is &t_word2. Should be &agreed_word2.";
  else put "File is fine";
run;

(and do note the dots at the end of each macro variable - always a good idea to do this).

sas-innovate-white.png

Join us for our biggest event of the year!

Four days of inspiring keynotes, product reveals, hands-on learning opportunities, deep-dive demos, and peer-led breakouts. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 734 views
  • 0 likes
  • 4 in conversation