BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
paperboy22
Calcite | Level 5

Hi all,

I am attempting to read in the results2.data data set so that it looks like the below output. However, when I try to I get an output that has the 4 variables below but only has values for source, and the values are 11, 11, 22,

 

The data residing in the results2.datPreview the document raw data file came from two different sources (1 and 2). If the value in column 10 is 1, the data in that record came from source 1. If the value in column 10 is 2, the data in that record came from source 2. In short, your job is to read the data file into a SAS data set called results2, so that when printed it looks like this:

homeworkL21_image03.gif

In order to do this, you'll need to:

  • Read in the source first. Tell SAS to hold the record in the input buffer.
  • Use an if source ... then input ... else input ... statement to tell SAS the appropriate order of the variables. If source= 1, then specify the variables in your INPUT statement in this order: id, name, source, and score. If source = 2, then specify the variables in your INPUT statement in this order: id, score, source, and name.

Then, print the resulting SAS data set.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

 

  1. Read the third field as SOURCE, with a trailing @  to hold the column pointer. The first two fields are read into a dummy variable, to be dropped later.
  2. Use the value of source to guide re-reading of the line.  BUT ...  you have to move the pointer back to column 1.  That's the "@1" in the code below.

 

DATA results2;
  input dummy :$6. dummy :$6. source @;
  if source=1      then input @1 id name $ source score;
  else if source=2 then input @1 id score source name $;
  drop dummy ;
DATALINES;
11 john 1 77
11 88 2 james
22 bobby 1 55
22 89 2 opey
RUN;

 

 

You  also could push (and hold) the pointer at column 1 in the first input.  Then the subsequent input's don't need the "@1".

 

DATA results2;
  input dummy :$6. dummy :$6. source @1 @;
  if source=1      then input id name $ source score;
  else if source=2 then input id score source name $;
  drop dummy  ;
DATALINES;
11 john 1 77
11 88 2 james
22 bobby 1 55
22 89 2 opey
RUN;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
ballardw
Super User

We can't see the example data. Copy some rows using a plain text editor and paste into a code box on the forum opened using the {I} icon.

 

You can use a trailing @ sign to hold the buffer on an input statement:

 

input source 10 @;

 

so you could use the value of source for different input statements:

if source = 1 then input <what ever is needed>;

else if source=2 then input <the other info>;

 

The details of the input are up to you.

paperboy22
Calcite | Level 5

Sorry, this is the data set: 

11 john 1 77
11 88 2 james
22 bobby 1 55
22 89 2 opey
 
This is the code I have written so far with the output it creates at the bottom: OPTIONS PS=58 LS=72 NODATE NONUMBER;
LIBNAME stat481 'C:\stat481\data';
DATA results2;
input source 10@;
if source=1 then input id name $ source score;
else if source=2 then input id score source name $;
DATALINES;
11 john 1 77
11 88 2 james
22 bobby 1 55
22 89 2 opey
;
RUN;
PROC PRINT Data=results2 NOOBS;
Title 'The results2 data set';
RUN;
 
Output
The results2 data set

source id name score

88 77 11 2
89 55 22 2

 
mkeintz
PROC Star

 

  1. Read the third field as SOURCE, with a trailing @  to hold the column pointer. The first two fields are read into a dummy variable, to be dropped later.
  2. Use the value of source to guide re-reading of the line.  BUT ...  you have to move the pointer back to column 1.  That's the "@1" in the code below.

 

DATA results2;
  input dummy :$6. dummy :$6. source @;
  if source=1      then input @1 id name $ source score;
  else if source=2 then input @1 id score source name $;
  drop dummy ;
DATALINES;
11 john 1 77
11 88 2 james
22 bobby 1 55
22 89 2 opey
RUN;

 

 

You  also could push (and hold) the pointer at column 1 in the first input.  Then the subsequent input's don't need the "@1".

 

DATA results2;
  input dummy :$6. dummy :$6. source @1 @;
  if source=1      then input id name $ source score;
  else if source=2 then input id score source name $;
  drop dummy  ;
DATALINES;
11 john 1 77
11 88 2 james
22 bobby 1 55
22 89 2 opey
RUN;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
paperboy22
Calcite | Level 5

That worked, thank you for your help.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1693 views
  • 1 like
  • 3 in conversation