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

I want to repeat each country's obs. for 31 times and the following statements run well.

%let country1=abudhabi;

%let country2=argentina;

%macro x;

data  %do i =1 %to 31;

                work.&country1&i

         %end;;

set    &country1;

run;

data  %do i =1 %to 31;

                work.&country2&i

         %end;;

set    &country2;

run;

%mend x;

%x;

But because I have tens of countries, I also want to apply the do loop on "countryj". The commands I want is something like the commands below but it failed. Can anyone give me some advice? Thanks.

%let country1=abudhabi;

%let country2=argentina;

%macro x;

%do j = 1 %to 2;

data  %do i =1 %to 31;

                work.&&country&j&i

         %end;;

set    &&country&j;

run;

%end;

%mend x;

%x;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Examine how the extra ampersand plays out.

&&country&i&j

The fist time through the loops, this becomes

&country11

And there is no such macro variable.  You would need to take advantage of using a dot to end a macro variable name, changing this to:

&&country&i..&j

On a secondary note, it might be easier to read the names if you change them slightly:

&&country&i.._&j

Good luck.

View solution in original post

2 REPLIES 2
Astounding
PROC Star

Examine how the extra ampersand plays out.

&&country&i&j

The fist time through the loops, this becomes

&country11

And there is no such macro variable.  You would need to take advantage of using a dot to end a macro variable name, changing this to:

&&country&i..&j

On a secondary note, it might be easier to read the names if you change them slightly:

&&country&i.._&j

Good luck.

p12937
Obsidian | Level 7

Looks like you all set, but here is another solution:

/* If you already have a data set with all the country names then you don't need this data step */
/* Just use your countries data set in the proc sql step. */
/* Otherwise, create a data set with all of your countries */
data countries;
country='argentina';output;
country='abudhabi';output;
run;

proc sql noprint;
select count(*)
   into :countries
   from countries
;
quit;

%macro x;

%do i=1 %to &countries.;

proc sql noprint;
  select country
    into :country
    from countries (firstobs=&i. obs=&i.)
  ;
quit;

%let country=%trim(%left(&country.));

/* You probably won't need this data step, I did just to make the code work */
data &country.;
  country="&country.";
run;

data
  %do j=1 %to 31;
  work.&country.&j.
  %end;
;
  set &country.;
run;

%end;

%mend x;

%x;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 848 views
  • 1 like
  • 3 in conversation