DATA Step, Macro, Functions and more

Create seperate file for each Name (ID)

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

Create seperate file for each Name (ID)

Hi Everyone,

 

I have a data with variable nameID as below.

I want to create separate file for each name AND the NameID colum is deleted from the final file.

Then export each file into CSV.

So the CSV file only contain var1 var2 (in the below dataset)

There are thousand names and I haven't found a way to do it yet.

Any help is very much appreciated.

 

HHC

 

data have;

input name_ID var1 var2;

datalines;

1111 2

111 3 6

111 6 9

555 2 2

555 4 5

555 5 4

563 2 2

563 1 1

;


Accepted Solutions
Solution
‎11-01-2015 12:05 AM
Respected Advisor
Posts: 4,649

Re: Create seperate file for each Name (ID)

Use FILEVAR= on the FILE statement to create your csv (example under Windows) :

 

data have;
input name_ID var1 var2;
datalines;
111 1 2
111 3 6
111 6 9
555 2 2
555 4 5
555 5 4
563 2 2
563 1 1
;

%let path=&sasforum.\Datasets;

data _null_;
set have;
myFilename = cats("&path.\", name_ID, ".csv");
file toto filevar=myFilename dsd;
put var1 var2;
run;
PG

View solution in original post


All Replies
Regular Contributor
Posts: 161

Re: Create seperate file for each Name (ID)

[ Edited ]

Hello,

Here is a code that can help..... 

data have;
input name_ID:$4. var1:$1. var2:$2.;
datalines;
1111 2
111 3 6
111 6 9
555 2 2
555 4 5
555 5 4
563 2 2
563 1 1
;
run;
proc sql;
	 create table t1 as select distinct name_ID from have;
quit;	 
proc print data=t1;
%macro names;
	       data &dsname;set have;where name_id = "&nameid";
%mend names;
data have;
      set t1;
      call symput('nameid',name_id);
      call symput ('dsname',("_"||name_id));
	  call execute ('%names');
run;
proc contents data=work._all_;

Since the values of the name_id are numbers, I have added an underscore in the beginning to allow SAS to create dataset with Char in the beginning... At the end, here is what it creates... Rows 11-14 is what you are interested in.

 

#	Name	Member Type	File Size	Last Modified
1	DATA1	DATA	128KB	10/31/2015 01:04:53
2	DATA2	DATA	128KB	10/31/2015 01:06:30
3	DATA3	DATA	128KB	10/31/2015 01:08:38
4	DATA4	DATA	128KB	10/31/2015 01:11:19
5	HAVE	DATA	128KB	10/31/2015 01:23:56
6	NAMEID	DATA	128KB	10/31/2015 01:01:31
7	REGSTRY	ITEMSTOR	32KB	10/31/2015 00:32:05
8	SASGOPT	CATALOG	12KB	10/31/2015 00:32:49
9	SASMAC1	CATALOG	188KB	10/31/2015 00:32:10
10	T1	DATA	128KB	10/31/2015 01:23:55
11	_111	DATA	128KB	10/31/2015 01:23:56
12	_1111	DATA	128KB	10/31/2015 01:23:56
13	_555	DATA	128KB	10/31/2015 01:23:56
14	_563	DATA	128KB	10/31/2015 01:23:56

Hope it helps someway.... Good luck...!!!

Kannan Deivasigamani
Super Contributor
Posts: 371

Re: Create seperate file for each Name (ID)

Thank you, Kannad for helping me.

HC

Solution
‎11-01-2015 12:05 AM
Respected Advisor
Posts: 4,649

Re: Create seperate file for each Name (ID)

Use FILEVAR= on the FILE statement to create your csv (example under Windows) :

 

data have;
input name_ID var1 var2;
datalines;
111 1 2
111 3 6
111 6 9
555 2 2
555 4 5
555 5 4
563 2 2
563 1 1
;

%let path=&sasforum.\Datasets;

data _null_;
set have;
myFilename = cats("&path.\", name_ID, ".csv");
file toto filevar=myFilename dsd;
put var1 var2;
run;
PG
Super Contributor
Posts: 371

Re: Create seperate file for each Name (ID)

Thank you, PG.

 


data have;
input name_ID var1 var2;
datalines;
1111 2
111 3 6
111 6 9
555 2 2
555 4 5
555 5 4
563 2 2
563 1 1
;
proc sort data=have;by name_ID; run;
data _null_;
set have;
fname=cats('c:\temp\File_',name_ID,'.csv');
file dummy filevar=fname dsd;
put var1 var2 ;
run;

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 228 views
  • 1 like
  • 3 in conversation