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

Greetings all

 

I have a simple SAS dataset with reference numbers for each observation, a variable for Zone (Zone1 or Zone 2) and then further data, something like this:

 

Reference numberZone Var 1Var 2Var 3 etc
102965952Zone2DataDataData
102987292Zone1DataDataData
103008632Zone1DataDataData
103029972Zone2DataDataData
103032102Zone2DataDataData
103033962Zone1DataDataData
103035902Zone2DataDataData
103037842Zone2DataDataData
103039782Zone2DataDataData
103041722Zone1DataDataData
103043662Zone2DataDataData
103045602Zone1DataDataData
103047542Zone2DataDataData

 

I would like to write a piece of code to divide this data into two datasets, split by zone and then export each dataset into the tab of a single spreadsheet with each tab having the name 'Zone1' or 'Zone2', drawing the name from the Zone variable, example excel sheet attached.

 

Any help gratefully received!

 

Thanks

Andy

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

One way

 

data have;
input RefNum Zone $ (Var1-Var3)($);
datalines;
102965952 Zone2 Data Data Data
102987292 Zone1 Data Data Data
103008632 Zone1 Data Data Data
103029972 Zone2 Data Data Data
103032102 Zone2 Data Data Data
103033962 Zone1 Data Data Data
103035902 Zone2 Data Data Data
103037842 Zone2 Data Data Data
103039782 Zone2 Data Data Data
103041722 Zone1 Data Data Data
103043662 Zone2 Data Data Data
103045602 Zone1 Data Data Data
103047542 Zone2 Data Data Data
;

proc sort data=have(keep=Zone) out=temp nodupkey; 
   by Zone;
run;

data callstack;
 length string $500;
 set temp;
 string=compbl(cats("
            proc export data=have(where=(Zone='", Zone, "'))
            			outfile= 'c:\Users\Peter\Desktop\MyExcelFile.xlsx'
            			dbms=xlsx replace;
            			sheet='", Zone, "';
            run;
           "));
 call execute(string);
run;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

One way

 

data have;
input RefNum Zone $ (Var1-Var3)($);
datalines;
102965952 Zone2 Data Data Data
102987292 Zone1 Data Data Data
103008632 Zone1 Data Data Data
103029972 Zone2 Data Data Data
103032102 Zone2 Data Data Data
103033962 Zone1 Data Data Data
103035902 Zone2 Data Data Data
103037842 Zone2 Data Data Data
103039782 Zone2 Data Data Data
103041722 Zone1 Data Data Data
103043662 Zone2 Data Data Data
103045602 Zone1 Data Data Data
103047542 Zone2 Data Data Data
;

proc sort data=have(keep=Zone) out=temp nodupkey; 
   by Zone;
run;

data callstack;
 length string $500;
 set temp;
 string=compbl(cats("
            proc export data=have(where=(Zone='", Zone, "'))
            			outfile= 'c:\Users\Peter\Desktop\MyExcelFile.xlsx'
            			dbms=xlsx replace;
            			sheet='", Zone, "';
            run;
           "));
 call execute(string);
run;
PeterClemmensen
Tourmaline | Level 20

Of course, change the path to suit your needs..

AJChamberlain
Obsidian | Level 7

Thanks Draycut, I'll give it a go!


A

PeterClemmensen
Tourmaline | Level 20

Anytime. Let me know how it goes 🙂

AJChamberlain
Obsidian | Level 7

Worked perfectly first time!

Thanks again

Andy

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 1212 views
  • 0 likes
  • 2 in conversation