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, } |
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;
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;
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.
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?)
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.