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

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,                                                                                       
}
]

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

3 REPLIES 3
Reeza
Super User

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;
wy110
Fluorite | Level 6

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.  

Astounding
PROC Star

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?)

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
  • 3 replies
  • 1971 views
  • 0 likes
  • 3 in conversation