BookmarkSubscribeRSS Feed
LavendarCat
Calcite | Level 5

I am just learning SAS and the assignment is to take a dataset of a certain format and turn it into another format. We are using SAS Studio.

Example:

BlackGrayRedWhite
57596078
67768150
52624766
74686860
55642459

Needs to look like:

ColorNumber
Black57
Gray59
Red60
White78
Black67
Gray76
Red81
White50

 

I have this so far but am stuck on what to put next. All the practicing we have done in class has been with datasets where they all read in rows where each row is related to each individual variable for that column. Not like here where the Color variable is at the top row and should be repeated with each number in that column. Any hints will be greatly appreciated. 

 

data Problem_3;
infile '/home/u63754936/STAT 725/Homework#1/Data Files HW1/Dataset3.txt';
input Color $ Number;

 


run;
proc print data=Problem_3;
title 'Problem_3';
run;

 

1 REPLY 1
Patrick
Opal | Level 21

Not sure where you are at in your learning journey so not sure if below code is already fitting for where you are at.

 

If you have an external text file then one way is to create the desired table structure as part of your data step that reads the table. 

data want;
  infile '/home/u63754936/STAT 725/Homework#1/Data Files HW1/Dataset3.txt'
    firstobs=2
    dsd
    dlm=' '
    truncover
    ;    
  length color $5;
  do color='Black','Grey','Red','White';
    input number @;
    output;
  end;
run;

proc print data=want;
run;

You will likely have to add a few options to the infile statement that instruct SAS how to read the external file - like for example dlm=.... to define the delimiting character used to separate the values.

 

If the do loop hasn't been covered yet in your classes then here another coding option.

data want;
  infile '/home/u63754936/STAT 725/Homework#1/Data Files HW1/Dataset3.txt'
    firstobs=2
    dsd
    dlm=' '
    truncover
    ;    
  length color $5;
  color='Black';
  input number @;
  output;
  color='Grey';
  input number @;
  output;
  color='Red';
  input number @;
  output;
  color='White';
  input number ;
  output;
run;

The most important bit in this code is the @ at the end of the input statement that allows you to use multiple input statements without the cursor jumping onto the next line of input. Look up the details in the SAS docu.

Patrick_0-1706504216152.png

 

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 1118 views
  • 0 likes
  • 2 in conversation