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

Hi Experts,

 

I have following code;

 


options mprint;
%let no = 1 2 3 4 5 6;
%macro samples (country);

%let pcoun = %sysfunc(countw(&&country)) ;
%do i = 1 %to &pcoun;
%do sta = 1 %to 3;

proc sql outobs=2;
create table selected_&country_&sta_&i as
select *
from mntly.joined_&country
where n = &i and input(stage,2.) = &sta;

%end;
%end;

%mend;

%samples(no);

I want tables with 2 entries for each portfolio and each stage. it has to create 18 tables. but it only creates 3 tables and stage 2 3 with empty tables.

 

I get following log

 

26         options mprint;
27         %macro samples (country);
28         
29         	%let pcoun = %sysfunc(countw(&&country)) ;
30         		%do i = 1 %to &pcoun;
31         			%do sta = 1 %to 3;
32         
33         proc sql outobs=2;
34         create table selected_&country._&sta._&i. as
35         select *
36         from mntly.joined_&country
37         where n = &i and input(stage,2.) = &sta;
38         
39         %end;
40         %end;
41         
42         %mend;
43         
44         %samples(no);
MPRINT(SAMPLES):   proc sql outobs=2;
MPRINT(SAMPLES):   create table selected_no_1_1 as select * from mntly.joined_no where n = 1 and input(stage,2.) = 1;
WARNING: Statement terminated early due to OUTOBS=2 option.
NOTE: Table WORK.SELECTED_NO_1_1 created, with 2 rows and 639 columns.

NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds
      

MPRINT(SAMPLES):   proc sql outobs=2;
MPRINT(SAMPLES):   create table selected_no_2_1 as select * from mntly.joined_no where n = 1 and input(stage,2.) = 2;
NOTE: Table WORK.SELECTED_NO_2_1 created, with 0 rows and 639 columns.
2                                                          The SAS System                               11:43 Tuesday, July 31, 2018


NOTE: PROCEDURE SQL used (Total process time):
      real time           7.81 seconds
      cpu time            7.59 seconds
      

MPRINT(SAMPLES):   proc sql outobs=2;
MPRINT(SAMPLES):   create table selected_no_3_1 as select * from mntly.joined_no where n = 1 and input(stage,2.) = 3;
NOTE: Table WORK.SELECTED_NO_3_1 created, with 0 rows and 639 columns.

45         
46         GOPTIONS NOACCESSIBLE;
47         %LET _CLIENTTASKLABEL=;
48         %LET _CLIENTPROCESSFLOWNAME=;
49         %LET _CLIENTPROJECTPATH=;
50         %LET _CLIENTPROJECTPATHHOST=;
51         %LET _CLIENTPROJECTNAME=;
52         %LET _SASPROGRAMFILE=;
53         %LET _SASPROGRAMFILEHOST=;
54         
55         ;*';*";*/;quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           7.82 seconds
      cpu time            7.68 seconds
      
55       !                run;

56         ODS _ALL_ CLOSE;
57         
58         
59         QUIT; RUN;
60         

 

 

Thanks in advance. 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Your output dataset name is determined solely by the outermost loop, so you will get only one result from the final iteration of the inner loops. Everything else will be overwritten.

You need to somehow incorporate &w and &sta into the dataset name.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Your output dataset name is determined solely by the outermost loop, so you will get only one result from the final iteration of the inner loops. Everything else will be overwritten.

You need to somehow incorporate &w and &sta into the dataset name.

PaigeMiller
Diamond | Level 26

Show us the SASLOG! Hint: Don't make us do work that SAS has already done. Hint: don't tell us it doesn't work, explain why it didn't work, what did you expect to get and what did you actually get.

 

Better yet, place 

 

options mprint;

as the first line of your code, run it again, and then show us the SASLOG. When you are going to paste the SASLOG into your message, click on the {i} icon first, paste the SASLOG into that window.

--
Paige Miller
Myurathan
Quartz | Level 8
I have followed your instruction and changed the code to run for only one country. Still, it is not producing 18 tables as I wanted. It produces only three tables and only one table with two observations.
gamotte
Rhodochrosite | Level 12

Hello,

 

@Kurt_Bremser gave you a good explanation of why you don't get the results

you want.

 

I am just curious about the following line in your code :

 

%let pcoun = %sysfunc(countw(&%scan(vlist,&i))) ;

What is it exactly supposed to do ?

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1573 views
  • 1 like
  • 4 in conversation