Git Product home page Git Product logo

stackdatamacro's Introduction

StackDataMacro

Generalized SAS macro that automatically converts a longitudinal dataset into a vertical dataset.

During my internship, I created two SAS macros to convert a longitudinal dataset into a vertical dataset. The stackMacro.sas script is for a dataset with a single key, and the stackMacroV2.sas script is for a dataset with a two-component key. The macros are completely generalized and require three parameters: the name of the dataset to be converted, the key(s) for that dataset, and the name of the new key for the transformed dataset. The macros also accept a list of variables to copy to the new dataset that would be lost in the transformation.

Ideally, one macro could be written to accept a list of keys so it would transform datasets with any number of keys.

The following section describes the version that handles a two-component key.

Description of stack macro: stackMacroV2.sas

The following image is the longitudinal dataset with two-component key: Brand and Store. The main metric variable is price, which is represented by hyphens in the week columns. This dataset also contains three variables that will need to be carried over from the original dataset to the vertical dataset: State, selected, and category.

Horizontal Data

Step 1: The first step in converting to a vertical format is to create new key variables.

When the dataset is transposed on the two fields that make up the key, the values contained in those fields will be concatentated to create a new variable. However, these fields may not contain SAS-compliant names. Though there are ways to handle non-SAS-compliant variable names that result from a transpose, I did not find a way to do this within a general program that needed to work with any dataset. Therefore, I created a dataset (below) to hold each new key component. The macro creates the new keys as numbered pairs to identify variable and row combinations as shown. The underscore allows for splitting the variable name later to incorporate the original component key values back into the dataset.

Temporary Variables

Step 2: Once the new keys are created, the macro creates a lookup table (below) to hold the original key components and any variables that would be lost in the transpose.

Lookup Table

Step 3: With SAS-compliant keys in place, the macro transposes the dataset on the new keys.

The macro then combines the date with each new variable to create multiple datasets to stack on top of each other. The lookup table is combined with the final vertical data and the temporary keys are deleted. The final dataset is displayed below.

Vertical Data

stackdatamacro's People

Contributors

lisaover avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.