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

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
Barite | Level 11

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
Barite | Level 11

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 4 replies
  • 948 views
  • 1 like
  • 3 in conversation