BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi All.

I want to create a dataset by parsing a fix length file and where each dataline contains the value for specific variables and not for all always.

e.g.
The target dataset column structure is:

Row# Type Name1 Name2 Name3 Amount1 Amount2 Amount3


The corresponding datalines in the file appears as:

A,John,1000
B,Mac,1200
C,Amar,1100
B,Jack,1300
B,Maria,1200
A,Meg,1400
C,Rohan,100
.
.
.


Rules:
* Each line contains one record for a dataset.
* Each line contains the data for only one Name variable and one Amount variable at the fix position.
* Based on the value of first variable(Type), the next values should get assigned to the corresponding variables as follow -

Type A --> Name1, Amout1 (rest all the variables in the dataset will be null)
Type B --> Name2, Amout2 (rest all the variables in the dataset will be null)
Type C --> Name3, Amout3 (rest all the variables in the dataset will be null)

* The datalines are shuffled and not in sequence of A,B,c compulsarily.
* The no. of dataliens are in millions.

The final dataset must look like -

Dataset:

Row# Type Name1 Name2 Name3 Amount1 Amount2 Amount3
1 A John 1000
2 B Mac 1200
3 C Amar 1100
4 B Jack 1300
5 B Maria 1200
6 A Meg 1400
7 C Rohan 1000
.
.
.



Solution:
In order to achieve above requirement, I have used the trailing "@" feature in the following code which is working quite ok.

Data .myDataset;
Infile ;
Input Type $1 @;
If Type = "A" then
Input Name1 $ Amount1;
If Type = "B" then
Input Name2 $ Amount2;
If Type = "C" then
Input Name3 $ Amount3;
run;

Query:
Just want to confirm it it is the only option to deal with such scenario. Considering the processing of million records, is there any other better alternative to achieve the above task.

Any help regarding this will be highly appreciated.

regards
Kapil Agrawal
1 REPLY 1
Olivier
Pyrite | Level 9
Hi Kapil.
This chaning-style row structure can only be imported in the way you discribe. I can just add a little change to your program, saving a few seconds on running time : change the IF to a SELECT statement.
[pre]
Data .myDataset;
Infile ;
Input Type $1 @;
SELECT (type) ;
WHEN("A") Input Name1 $ Amount1;
WHEN("B") Input Name2 $ Amount2;
WHEN("C") Input Name3 $ Amount3;
OTHERWISE INPUT ;
run;
[/pre]
Regards.
Olivier

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
  • 1 reply
  • 514 views
  • 0 likes
  • 2 in conversation