How to convert a SAS data set to a data step

2019-05-28 19:58发布

How can I convert my SAS data set, into a data set that I can easily paste into the forum or hand over to someone to replicate my data. Ideally, I'd also like to be able to control the amount of records that are included.

Ie I have sashelp.class in the SASHELP library, but I want to provide it here so others can use it as the starting point for my question.

标签: sas
2条回答
做个烂人
2楼-- · 2019-05-28 20:21

This topic came up recently on SAS Communities and I created a little more robust macro than the one Reeza linked. You can see it in Github: ds2post.sas

* Pull macro definition from GITHUB ;
filename ds2post url
  'https://raw.githubusercontent.com/sasutils/macros/master/ds2post.sas'
;
%include ds2post ;

For example if you wanted to share the first 5 observations of SASHELP.CARS you would run this macro call:

%ds2post(sashelp.cars,obs=5)

Which would generate this code to the SAS log:

data work.cars (label='2004 Car Data');
  infile datalines dsd dlm='|' truncover;
  input Make :$13. Model :$40. Type :$8. Origin :$6. DriveTrain :$5.
    MSRP Invoice EngineSize Cylinders Horsepower MPG_City MPG_Highway
    Weight Wheelbase Length
  ;
  format MSRP dollar8. Invoice dollar8. ;
  label EngineSize='Engine Size (L)' MPG_City='MPG (City)'
    MPG_Highway='MPG (Highway)' Weight='Weight (LBS)'
    Wheelbase='Wheelbase (IN)' Length='Length (IN)'
  ;
datalines4;
Acura|MDX|SUV|Asia|All|36945|33337|3.5|6|265|17|23|4451|106|189
Acura|RSX Type S 2dr|Sedan|Asia|Front|23820|21761|2|4|200|24|31|2778|101|172
Acura|TSX 4dr|Sedan|Asia|Front|26990|24647|2.4|4|200|22|29|3230|105|183
Acura|TL 4dr|Sedan|Asia|Front|33195|30299|3.2|6|270|20|28|3575|108|186
Acura|3.5 RL 4dr|Sedan|Asia|Front|43755|39014|3.5|6|225|18|24|3880|115|197
;;;;

Try this little test to compare the two macros.

First make a sample dataset with a couple of issues.

data testit;
  set sashelp.class (obs=5);
  if _n_=1 then name='Le Bron';
  if _n_=2 then age=.;
  if _n_=3 then wt=.;
  if _n_=4 then name='12;34';
run;

Then run both macros to dump code to the SAS log.

%ds2post(testit);
%data2datastep(dsn=testit,obs=20);

Copy the code from the log. Changing the name in the DATA statements to not overwrite the original dataset or each other. Run them and compare the result to the original.

proc compare data=testit compare=testit1; run;
proc compare data=testit compare=testit2; run;

Result using %DS2POST:

The COMPARE Procedure
Comparison of WORK.TESTIT with WORK.TESTIT1
(Method=EXACT)

Data Set Summary

Dataset                Created          Modified  NVar    NObs

WORK.TESTIT   02NOV18:17:09:40  02NOV18:17:09:40     6       5
WORK.TESTIT1  02NOV18:17:10:29  02NOV18:17:10:29     6       5

Variables Summary

Number of Variables in Common: 6.

Observation Summary

Observation      Base  Compare

First Obs           1        1
Last  Obs           5        5

Number of Observations in Common: 5.
Total Number of Observations Read from WORK.TESTIT: 5.
Total Number of Observations Read from WORK.TESTIT1: 5.

Number of Observations with Some Compared Variables Unequal: 0.
Number of Observations with All Compared Variables Equal: 5.

Summary of results using %Data2DataStep:

Comparison of WORK.TESTIT with WORK.TESTIT2
(Method=EXACT)

Data Set Summary

Dataset                Created          Modified  NVar    NObs

WORK.TESTIT   02NOV18:17:09:40  02NOV18:17:09:40     6       5
WORK.TESTIT2  02NOV18:17:10:29  02NOV18:17:10:29     6       3


Variables Summary

Number of Variables in Common: 6.


Observation Summary

Observation      Base  Compare

First Obs           1        1
First Unequal       1        1
Last  Unequal       3        3
Last  Match         3        3
Last  Obs           5        .

Number of Observations in Common: 3.
Number of Observations in WORK.TESTIT but not in WORK.TESTIT2: 2.
Total Number of Observations Read from WORK.TESTIT: 5.
Total Number of Observations Read from WORK.TESTIT2: 3.

Number of Observations with Some Compared Variables Unequal: 3.
Number of Observations with All Compared Variables Equal: 0.

Variable Values Summary

Values Comparison Summary

Number of Variables Compared with All Observations Equal: 1.
Number of Variables Compared with Some Observations Unequal: 5.
Number of Variables with Missing Value Differences: 4.
Total Number of Values which Compare Unequal: 12.
Maximum Difference: 0.


Variables with Unequal Values

Variable  Type  Len  Ndif   MaxDif  MissDif

Name      CHAR    8     1                 0
Sex       CHAR    1     3                 3
Age       NUM     8     2        0        2
Height    NUM     8     3        0        3
Weight    NUM     8     3        0        3

Note that I am sure there are values that will cause trouble for my macro also. But hopefully they are caused by data that is less likely to occur than spaces or semi-colons.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-05-28 20:27

To do this, you can use a macro written by Mark Jordan at SAS, the code is stored in GitHub as well.

You need to provide the data set name, including library and the number of observations you want to output. It takes them in order. The code will then appear in your SAS log.

*data set you want to create demo data for;
%let dataSetName = sashelp.Class;
*number of observations you want to keep;
%let obsKeep = 5;


******************************************************
DO NOT CHANGE ANYTHING BELOW THIS LINE
******************************************************;

%let source_path = https://gist.githubusercontent.com/statgeek/bcc55940dd825a13b9c8ca40a904cba9/raw/865d2cf18f5150b8e887218dde0fc3951d0ff15b/data2datastep.sas;

filename reprex url "&source_path";
%include reprex;
filename reprex;

option linesize=max;
%data2datastep(dsn=&dataSetName, obs=&obsKeep);

This may not work if you do not have access to the github page, in that case, you can manually navigate to the page (same link) and copy/paste it into SAS. Then run the program and run only the last step, the %data2datastep(dsn=, obs=);

查看更多
登录 后发表回答