banner



How To Create A Data Set In Sas

About Creating a SAS Data Gear up with a Information Step

Y'all can create either a SAS data file, a data gear up that holds bodily data, or a SAS view, a data prepare that references data that is stored elsewhere. By default, you create a SAS data file. To create a SAS view instead, utilise the VIEW= choice in the Information statement. With a SAS view you can, for case, procedure monthly sales figures without having to edit your DATA step. Whenever you need to create output, the output from a SAS view reflects the current input data values.

The following DATA statement creates a SAS view called MONTHLY_SALES.

data monthly_sales / view=monthly_sales;

The following Data statement creates a information file called TEST_RESULTS.

data test_results;

Yous select data-reading statements based on the source of your input information. There are at least six sources of input data:

  • raw data in an external file

  • raw data in the jobstream (instream data)

  • data in SAS information sets

  • data that is created by programming statements

  • data that you lot can remotely access through an FTP protocol, TCP/IP socket, a SAS catalog entry, or through a URL

  • information that is stored in a Database Management Organization (DBMS) or other vendor'due south data files.

Usually Data steps read input information records from simply one of the first three sources of input. However, Data steps tin utilise a combination of some or all of the sources.


Example 1: Reading External File Information

The components of a DATA step that produce a SAS data set from raw data stored in an external file are outlined hither.

data weight;          1          infile 'your-input-file';          2          input IDnumber $ Week1 Week16;          three          WeightLoss=Week1-Week16;          four          run;          v          proc impress data=weight;          six          run;          seven        

[1] Begin the Information pace and create a SAS data ready called WEIGHT.

[2] Specify the external file that contains your data.

[3] Read a tape and assign values to 3 variables.

[4] Calculate a value for variable WeightLoss.

[5] Execute the DATA step.

[6] Print information gear up WEIGHT using the Print procedure.

[7] Execute the PRINT procedure.


Example ii: Reading Instream Data Lines

This example reads raw data from instream data lines.

data weight2;          1          input IDnumber $ Week1 Week16;          2          WeightLoss2=Week1-Week16;          3          datalines;          iv          2477 195  163 2431 220  198 2456 173  155 2412 135  116 ;          v          proc print data=weight2;          vi          run;          vii        

[1] Brainstorm the DATA step and create SAS data prepare WEIGHT2.

[2] Read a information line and assign values to iii variables.

[3] Calculate a value for variable WeightLoss2.

[4] Brainstorm the data lines.

[5] Betoken end of data lines with a semicolon and execute the Data stride.

[6] Print data set WEIGHT2 using the PRINT process.

[7] Execute the PRINT procedure.


Example iii: Reading Instream Data Lines with Missing Values

You lot can also take advantage of options in the INFILE statement when you read instream data lines. This example shows the use of the MISSOVER option, which assigns missing values to variables for records that contain no data for those variables.

data weight2;      infile datalines missover;          ane          input IDnumber $ Week1 Week16;      WeightLoss2=Week1-Week16;    datalines;          2          2477 195  163 2431  2456 173  155 2412 135  116 ;          3          proc print data=weight2;          four          run;          5        

[1] Use the MISSOVER option to assign missing values to variables that practise non contain values in records that exercise non satisfy the current INPUT statement.

[2] Begin data lines.

[3] Signal end of information lines and execute the Information step.

[4] Print data set WEIGHT2 using the Impress procedure.

[5] Execute the Impress procedure.


Instance iv: Using Multiple Input Files in Instream Data

This example shows how to employ multiple input files as instream data to your program. This case reads the records in each file and creates the ALL_ERRORS SAS information gear up. The program and then sorts the observations past Station, and creates a sorted information prepare chosen SORTED_ERRORS. The print process prints the results.

options pageno=1 nodate linesize=60 pagesize=80;  information all_errors;    length filelocation $ lx;    input filelocation;  /* reads instream data */    infile daily filevar=filelocation                 filename=daily end=done;    do while (not done);       input Station $ Shift $ Employee $ NumberOfFlaws;       output;    end;    put 'Finished reading ' daily=;    datalines; . . .myfile_A. . . . . .myfile_B. . .  . . .myfile_C. . .   ;  proc sort data=all_errors out=sorted_errors;    by Station; run;  proc print data = sorted_errors;    title 'Flaws Written report sorted past Station'; run;

Multiple Input Files in Instream Data

          Flaws Report sorted by Station                        1                                                           Number                 Obs    Station     Shift    Employee    OfFlaws                    1    Amherst       2       Lynne         0                      2    Goshen        ii       Seth          4                      3    Hadley        ii       Jon           iii                      4    Holyoke       1       Walter        0                      5    Holyoke       1       Affront          three                      half dozen    Orange        ii       Carol         5                      7    Otis          i       Kay           0                      8    Pelham        2       Mike          4                      9    Stanford      ane       Sam           ane                     ten    Suffield      2       Lisa          1        

This case reads data from 1 SAS information set, generates a value for a new variable, and creates a new data set.

information average_loss;          1          set weight;          2          Per centum=round((AverageLoss * 100) / Week1);          3          run;          iv        

[1] Brainstorm the DATA stride and create a SAS data set chosen AVERAGE_LOSS.

[2] Read an observation from SAS data fix WEIGHT.

[3] Calculate a value for variable Per centum.

[4] Execute the Information step.

You can create information for a SAS information set by generating observations with programming statements rather than by reading data. A Data pace that reads no input goes through only one iteration.

information investment;          1          brainstorm='01JAN1990'd;    cease='31DEC2009'd;    practice year=yr(begin) to year(terminate);          2          Capital+2000 + .07*(Upper-case letter+2000);       output;          iii          stop;    put 'The number of Data step iterations is '_n_;          4          run;          5          proc print data=investment;          half dozen          format Capital dollar12.2;          7          run;          8        

[1] Begin the DATA step and create a SAS data set called INVESTMENT.

[2] Summate a value based on a $2,000 capital investment and 7% involvement each yr from 1990 to 2009. Calculate variable values for one observation per iteration of the DO loop.

[3] Write each ascertainment to data set INVESTMENT.

[4] Write a notation to the SAS log proving that the Information step iterates but in one case.

[5] Execute the Data step.

[6] To see your output, print the INVESTMENT information set with the PRINT procedure.

[7] Use the FORMAT argument to write numeric values with dollar signs, commas, and decimal points.

[8] Execute the PRINT process.

Previous Page | Next Page | Top of Page

Source: https://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/a000998889.htm

0 Response to "How To Create A Data Set In Sas"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel