BookmarkSubscribeRSS Feed
uspanchal
Fluorite | Level 6

I need to write following same code with using macro code

 

data test;
do n = 1 to 5;
output;
end;
run;

data comp;
set test;
do k=1 to 3 ;
array d (3);
do i=1 to k ;
if n >= i then d(i) = 1;
end;
output;
end;
run;

10 REPLIES 10
uspanchal
Fluorite | Level 6

not understand from reply. 

LinusH
Tourmaline | Level 20

What @Kurt_Bremser is trying to say, is he don't understand why you to make this into macro code - there is no obious need.

So can you please state your objective?

 

Side note: in macro language, the default behaviour is to treat everything as text, not numerical values.

Data never sleeps
ErikLund_Jensen
Rhodochrosite | Level 12

@uspanchal 

Would you care to explain your problem. It might be possible to simulate a data step using I/O functions in a macro, but it would be downright stupid to do it, so what are you trying to achieve?

uspanchal
Fluorite | Level 6

I want to compare numeric variable of data set using macro code so I need input how to write macro code for compare numeric value.

Kurt_Bremser
Super User

Forget your macro obsession. You want to deal with data, not dynamically generated program code, so no macro action is needed.

Comparisons of variables within one dataset is done in a data step (or proc sql), and comparison across different datasets is done with proc compare.

 

For detailed suggestions/solutions, provide existing data in usable form (data step(s) with datalines), and what you expect out of it.

uspanchal
Fluorite | Level 6

here with attached dataset file HTWT

 

 

Write a macro to compare two numeric values.


 Use proc print and Use %if %then %do statement

PaigeMiller
Diamond | Level 26

@uspanchal wrote:

I want to compare numeric variable of data set using macro code so I need input how to write macro code for compare numeric value.


Macro language is a text processor, to help you make your code dynamic so it changes as circumstances change. Numeric calculations should be performed in a data step or PROC, not in macro language.

--
Paige Miller
Astounding
PROC Star

Macro language can easily compare two numeric values.  If you have two macro variables (in this example, &N and &K), and they both contain integers only, you can use:

 

%if &n > &k %then %do;

 

However, so far you do not have any macro variables.  It's not clear what you are trying to compare.

 

If you need to use macro language, you need to learn macro language.  Read a book.  Take a course.  You cannot learn macro language by asking a question or two on a message board.  To give you some examples ...

 

What if your macro variables do not contain integers?  Decimal points are not considered integers.  Macro language returns the opposite result for these two comparisons:

 

%if 10 > 4 %then %do;

%if 10. > 4. %then %do;

 

While there are ways to handle this, the real message is that you need to learn macro language.  Without that knowledge, any answers you get are dangerous to apply.

 

Relating to your original post ...

 

There is no equivalent to a DATA statement in macro language.

 

There is no equivalent to a SET statement in macro language.

 

There is no equivalent to an ARRAY statement in macro language.  (Skillful macro programmers can use macro programming statements to mimic the behavior of an array.)

 

There is no equivalent to an OUTPUT statement in macro language.

 

%IF %THEN statements almost always have to appear within a macro definition.  (It's not clear that you know what that means, and that would also be basic information you would learn from a book or a course.)

 

Your first task in tackling your homework is to find out what the question means.  Your .csv file contains no macro variables.  So what are you supposed to compare?

uspanchal
Fluorite | Level 6
thanks a lot very useful information

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 10 replies
  • 3371 views
  • 1 like
  • 6 in conversation