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

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

;

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

4 REPLIES 4
kannand
Lapis Lazuli | Level 10

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
hhchenfx
Rhodochrosite | Level 12

Thank you, Kannad for helping me.

HC

PGStats
Opal | Level 21

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
hhchenfx
Rhodochrosite | Level 12

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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