BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Flexymt
Calcite | Level 5

Hey,

I just need a little bit help with some macro coding.

I have a random sample of 30 observations from the full dataset of 21000. The first code you see is where i create a macro variable for each of the 30 observations from the sample.

proc sql noprint;

select X

into :X1-X30

from DatasetSample;

quit;

The second code is posted below. The point of this loop macro variable is to create 30 unique datasets by using the values from the macro variables X1-X30.
The problem arises in the "where" statement. For the first loop i want the where statement to look like : X=X1, for the second loop i want it to look like X=X2 and so on.

%macro sql(n)


%do r=1 %to &n;

proc sql;

create table Company&r as

select *

from DatasetTotal

where X=&X&r;           /* This code right here doesnt work. By inserting n=30 i thought the loop would automically assume X=X1, X=X2, X=3 and so on*/

quit;

%end;

%mend;

% SQL(30)

Any tips or help would be greatly appreciated.


1 ACCEPTED SOLUTION

Accepted Solutions
JonasE
Calcite | Level 5

Try changing &X&r to &&X&r. Should do the trick.

View solution in original post

2 REPLIES 2
JonasE
Calcite | Level 5

Try changing &X&r to &&X&r. Should do the trick.

Flexymt
Calcite | Level 5

Thanks so much! You are the best!

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
  • 2 replies
  • 782 views
  • 1 like
  • 2 in conversation