BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Am trying to create three new indicator variables k_200907, k_200908 and k_200909 into dataset B via macro below, but only created variable k_200909 in the end.


%macro naruto(var1=);

data work.datasetB;
set work.datasetA;
if date < &var1 then k_&var1 = 1;
else if date >= &var1 then k_&var1 = 0;
run;

%mend naruto;

%naruto(var1 = 200907)
%naruto(var1 = 200908)
%naruto(var1 = 200909)
run;

Please could anyone shed some light on why variable k_200907 and k_200908 aren't stored in dataset B? Many thanks. Regards Silverflute
4 REPLIES 4
Cynthia_sas
SAS Super FREQ
Hi:
Simply put, the SAS Macro Facility is like a big, automatic typewriter. Every time you invoke your macro program (%NARUTO), the Macro word scanner and tokenizer is typing the WHOLE DATA step program over again.

So the first invocation creates the resolved code and when the resolved code runs, WORK.DATASETB is created. However, the SECOND invocation again types the DATA step code, with the new values and now THAT resolved code is compiling and executing and is writing OVER the first WORK.DATASETB. And THEN, the same thing happens again the THIRD time you invoke your macro program. So what you're left with is the 3rd version of WORK.DATASETB.

I would recommend that you read up a bit on how SAS Macros work by searching for previous forum postings or using Google to search for SAS Macro tutorials from user groups. Here's a paper to get you started:
http://www2.sas.com/proceedings/sugi28/056-28.pdf

If you want to create variable k_200907 and k_200908 and k_200909 in the same dataset, you will have to redesign how your macro program works and how it generates the DATA step program. Here's a hint, a macro program can generate WHOLE steps, such as whole procedure steps or whole DATA step programs or a macro program can generate parts of statements within a procedure or program or can generate multiple statements. Your macro program does NOT have to contain an entire complete DATA step program to accomplish what you want to do. But this also means that your macro invocation might have to change.

cynthia
deleted_user
Not applicable
Many thanks Cynthia
SASPhile
Quartz | Level 8
when you pass var1 = 200907 then only k_200907 is created.
the next time k_200907 will be over written by other values that you pass(200908,200909).
so at any given time with your logic only one variable will be created.

try to pass three parameters and var1=, var2=, var3= and add the logic in the code in the if-then else statement.
deleted_user
Not applicable
Many thanks SAS Phile

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
  • 4 replies
  • 881 views
  • 0 likes
  • 3 in conversation