28014

SAS how to create variable with corresponding values efficiently

Question:

I am trying to complete the following.

Variable Letter has three values (a, b, c). I would like to create a variable Letter_2 with values corresponding to the values of Letter, namely (1, 2, 3).

I know I can do this using three IF Then statements.

if Letter='a' then Letter_2='1'; if Letter='b' then Letter_2='2'; if Letter='c' then Letter_2='3';

Suppose I have 15 values for the variable Letter, and 15 corresponding values for the replacement. Is there a way to do it efficiently without typing the same If Then statement 15 times?

I am new to SAS. Any clue will be appreciated.

Lisa

Answer1:

Looks like an application for a FORMAT.

First define the format.

proc format ; value $lookup 'a'='1' 'b'='2' 'c'='3' ; run;

Then use it to re-code your variable.

data want; set have; letter2 = put(letter,$lookup.); run;

Or perhaps you could use two temporary arrays and the WHICHC() function?

data have; input letter $10. ; cards; car apple box ;;;; data want ; set have ; array from (3) $10 _temporary_ ('apple','box','car'); array to (3) $10 _temporary_ ('first','second','third'); if whichc(letter,of from(*)) then letter_2 = to(whichc(letter,of from(*))) ; run;

Recommend

  • combine 2 associative arrays where values match
  • Select distinct where date is max
  • SQL Server - Splitting a string
  • How do I restrict the creation of an anonymous subclass (Java)
  • GQL query help - How can I write a query with where clause in GQL ? I am using google appengine data
  • How do I create a cumulative median field in SAS VA 7.4 Designer?
  • Reading data from a SAS data source in .Net
  • Map predictions back to IDs - Python Scikit Learn DecisionTreeClassifier
  • FOSUserBundle force user to write a different password
  • Is there a way to ensure one object reference per record in an ActiveRecord hierarchy?
  • Ruby: Why does this way of using map throw an error?
  • How to make stdcall from Go
  • How to enable large page memory for the JVM?
  • .NET video play library which allows to change the playback rate?
  • How to 'create temp table as select' in Slick?
  • Casting between Interfaces and Classes
  • MySQL Order by column = x, column asc?
  • What and where is mdimport
  • Custom Tabgroup Appcelerator
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Atlas images wrong size on iPad iOS 9
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Spring Data JPA custom method causing PropertyReferenceException
  • Projection media query: browser support and workarounds?
  • NetLogo BehaviorSpace - Measure runs using reporters
  • What is the “return” in scheme?
  • Different response to non-authenticated users and AJAX calls
  • C# - Serializing and deserializing static member
  • Sending data from AppleScript to FileMaker records
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • ActionScript 2 vs ActionScript 3 performance
  • Weird JavaScript statement, what does it mean?
  • Font Awesome Showing Box instead of Icons
  • Google cloud sdk not working when python points python3
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • How to include full .NET prerequisite for Wix Burn installer
  • Is it possible to post an object from jquery to bottle.py?
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal