BookmarkSubscribeRSS Feed
praveenkotte
Fluorite | Level 6

I want to create new data set for each obseravtion from sashelp.class using dynamic macros code?

 

 

hot coding:

data ds ds1 ds2 ds3----- ds19;

set sashelp.class;

if _n_=1 then output ds;

else if _N_=2 then output ds1;

 and so on

else output ds19;

run ;

 

 

 

i want to perform above task to be throught macros

 

 

9 REPLIES 9
praveenkotte
Fluorite | Level 6

it is one of the interview question that i was asked in one company.

 

Reeza
Super User

Sometimes interviewers want to hear -I would never do that 🙂 

Kurt_Bremser
Super User

@Reeza wrote:

Sometimes interviewers want to hear -I would never do that 🙂 


That put a smile on my face. Been there, done that, from both sides (interviewer and interviewee).

user24feb
Barite | Level 11

I think it is actually a proc-sql-select-into-question then:

Proc SQL NoPrint;
  Select Max(NObs) Into :NObs Trimmed From (Select Count(*) As NObs From Sashelp.Class);
Quit;

%Macro OutputSingleLine;
%Do i=1 %To &NObs.;
Data DS&i.;
  Set Sashelp.Class (Firstobs=&i. Obs=&i.);
Run;
%End;
%Mend;
%OutputSingleLine;
Astounding
PROC Star

As Kurt mentioned, it is unlikely that there is a good technical reason to do this.  However, if this is just an exercise to get practice in macro programming, it could be useful.

 

Remember that macro language is only going to generate the same SAS language statements that you already have coded.  So inside of a macro you could generate the DATA statement like this:

 

%local i;

data ds %do i=1 %to 19;

            ds&i

        %end;

        ;

 

It's the same DATA statement, but you don't have to type it out.  You can let macro language generate the statement for you.

 

Similarly, you can use a macro loop to generate the OUTPUT statements.  Since you've now seen one loop for the DATA statement, I'll leave the OUTPUT statements to you.

user24feb
Barite | Level 11

I think that every macro solution is more complicated than a data step. Try:


Data _NULL_;
  Set Sashelp.Class;
  i+1;
  Call Execute("Data xxx"!!Strip(Put(i,Best.))!!";");
  Call Execute("  Set Sashelp.Class (Firstobs="!!Strip(Put(i,Best.))!!" Obs="!!Strip(Put(i,Best.)) !!");");
  Call Execute("Run;");
Run;
Kurt_Bremser
Super User

The advantage of @user24feb's solution is that it does scale well.

Trying to do such an operation in a single data step will sooner or later crack the limit for a single SAS statement (32767 characters) when enough observations in the input will cause a too large data statement.

Ksharp
Super User

Or No need a MACRO .

 

data _null_;
 if _n_=1 then do;
  if 0 then set sashelp.class(obs=0);
  declare hash h(dataset:'sashelp.class(obs=0)');
  h.definekey(all:'y');
  h.definedata(all:'y');
  h.definedone(); 
 end;
set sashelp.class;
h.add();
h.output(dataset:cats('ds',_n_));
h.clear();
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 9 replies
  • 1461 views
  • 4 likes
  • 6 in conversation