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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1261 views
  • 0 likes
  • 3 in conversation