DATA Step, Macro, Functions and more

how to print multiple data sets with do loop

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

how to print multiple data sets with do loop

[ Edited ]

Hi,

 

I have some data sets: work.data1, work.data2, work.data3 .... work.data1000. I would like to output them with do loop. 

 

I found a similar question(https://communities.sas.com/t5/Base-SAS-Programming/Using-a-do-loop-to-load-multiple-data-sets/m-p/3...) and wrote following SAS codes, however, it doesn't work out correctly.

 

data work.data1;
input person_id claim_id $ var1 var2 var3;
datalines;
1 ab 12 23 34
1 cc 34 45 32
1 ee 56 32 55
run;

data work.data2;
input person_id claim_id $ var1 var2 var3;
datalines;
2 gh 11 22 23 
2 cd 34 67 43
;
run;




%macro Data;

%local i;
%do i=1 %to 2;

data _null_;
file print PS = 32767;

set work.data&i end=lastrec;

if _N_ eq 1 then do;
put '['; put ' "PERSON_ID":' PERSON_ID +(-1)','; end; put '{'; put ' "claim_id":' claim_id +(-1)','; put ' "days":' var2 +(-1)','; if lastrec eq 1 then do; put '}';
put ']'; end; else do; put '},'; end; run; %end; %mend; %Data;

 

It prints out separately. There are two outputs.

 

I only want one whole output, like following:

Any help will be appreciated!!!

 

SAS Output

[                                                                                                 
 "PERSON_ID":1,                                                                                   
{                                                                                                 
 "claim_id":ab,                                                                                   
 "days":23,                                                                                       
},                                                                                                
{                                                                                                 
 "claim_id":cc,                                                                                   
 "days":45,                                                                                       
},                                                                                                
{                                                                                                 
 "claim_id":ee,                                                                                   
 "days":32,                                                                                       
}                                                                                                 
],
[                                                                                                 
 "PERSON_ID":2,                                                                                   
{                                                                                                 
 "claim_id":gh,                                                                                   
 "days":22,                                                                                       
},                                                                                                
{                                                                                                 
 "claim_id":cd,                                                                                   
 "days":67,                                                                                       
}
]

 

 


Accepted Solutions
Solution
‎08-14-2017 12:28 PM
Super User
Posts: 23,980

Re: how to print multiple data sets with do loop

I don't think you need a macro, or if you do the loop needs to be defined differently for sure. 

Here's one that I think does what you want. Note that I redirected this to an output file, since I assume that's the end goal anyways.

 

data combined / view=combined;
set data1-data2;
run;


data _null_;
file 'c:\_localdata\temp.txt';

set combined end=lastrec;
by person_id notsorted;

if first.person_id then do;
put '[';
put ' "PERSON_ID":' PERSON_ID +(-1)',';
end;
put '{';
put ' "claim_id":' claim_id +(-1)',';
put ' "days":' var2 +(-1)',';


if lastrec eq 1 then do;
put '}';put ']';
end;

else do;
put '},';
end;

run;

View solution in original post


All Replies
Solution
‎08-14-2017 12:28 PM
Super User
Posts: 23,980

Re: how to print multiple data sets with do loop

I don't think you need a macro, or if you do the loop needs to be defined differently for sure. 

Here's one that I think does what you want. Note that I redirected this to an output file, since I assume that's the end goal anyways.

 

data combined / view=combined;
set data1-data2;
run;


data _null_;
file 'c:\_localdata\temp.txt';

set combined end=lastrec;
by person_id notsorted;

if first.person_id then do;
put '[';
put ' "PERSON_ID":' PERSON_ID +(-1)',';
end;
put '{';
put ' "claim_id":' claim_id +(-1)',';
put ' "days":' var2 +(-1)',';


if lastrec eq 1 then do;
put '}';put ']';
end;

else do;
put '},';
end;

run;
Occasional Contributor
Posts: 10

Re: how to print multiple data sets with do loop

[ Edited ]

Thank you sooo much!!! I was thinking too much!!! You are right! I even subsetted a giant data set to multiple datasets by their ID in order to print out them.  

Super User
Posts: 6,921

Re: how to print multiple data sets with do loop

Potentially, you might be omitting important information here.  Some questions:

 

Does each data set contain the data for just a single PERSON_ID?  (If there are multiple PERSON_IDs within a single data set, is each data set in order BY PERSON_ID?)

 

Is it possible that multiple data sets refer to the same PERSON_ID?  (If so, should the report for that PERSON_ID list the person only once, grouping all claims from all data sets below?)

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 227 views
  • 0 likes
  • 3 in conversation