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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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