BookmarkSubscribeRSS Feed
Saurav
Fluorite | Level 6

for given data:

 

11 john  1 77
11 88    2 james
22 bobby 1 55
22 89    2 opey

 

question.JPG

 

also , i coded as follows, though program works perfect, but I want to know why ((then input @1 id @4 score @12 name $5. @12 name $5. @10 source 1.;)) requires direct pointing , even id variable requires it?

 

 

dm"log;clear;output;clear";

DATA result2;
input @10 source 1.@; *recording in input buffer source ;

if source = 1 then input @1 id @4 name $5. @12 score @12 score @10 source 1. ;
if source = 2 then input @1 id @4 score @12 name $5. @12 name $5. @10 source 1.;

datalines;
11 john  1 77
11 88    2 james
22 bobby 1 55
22 89    2 opey
;

RUN;

PROC PRINT data = result2 noobs;
var source id name score;
RUN;



[Edit by Kurt Bremser] Moved program text to code window, corrected datalines so they match the input statement column pointers


question.JPG
3 REPLIES 3
Kurt_Bremser
Super User

After the first input statement, that stays in the same input line because of the trailing @, the column pointer stays where it was after reading source.

Therefore, the column pointer must be set to the beginning of the line before attempting to read id.

Kurt_Bremser
Super User

PS you could simplify your code like that:

data result2;
input @10 source 1. @1 @; *recording in input buffer source ;
if source = 1 then input id @4 name $5. @12 score @12 score @10 source 1. ;
if source = 2 then input id @4 score @12 name $5. @12 name $5. @10 source 1.;
datalines;
11 john  1 77
11 88    2 james
22 bobby 1 55
22 89    2 opey
;
run;
Reeza
Super User

You input file a fixed width file so each variable starts at a specified column. Consider how it handles a name such as John Paul. 

It has two layouts sending on source column. I think your input statements for source is incorrect as you read name/score twice. 

Your sample data posted doesn't reflect this because of formatting in forum most likely. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1253 views
  • 0 likes
  • 3 in conversation