BookmarkSubscribeRSS Feed
ramm
Calcite | Level 5

Hi Everyone,

 

I have a text file that is similar to the one below (1). It does not have any demiliers and it has the variables: name,sex,age,weight, height. Could you kindly help with how this can be imported into a SAS dataset as shown in (2) ?

 

I)

External text file:

 

JanetF1562.5112.5
MaryF1566.5112
CarolF1462.8102.5
JudyF1464.390

 

2)

Dataset table after importing into SAS should look like:

 

Janet F 15 62.5 112.5
Mary F 15 66.5 112
Carol F 14 62.8 102.5
Judy F 14 64.3 90

 

Thank you so much,

 

Regards,

ramm

5 REPLIES 5
PGStats
Opal | Level 21

PRX to the rescue! Assuming that age always has two digits and weight always has a single decimal :

 

 

data test;
length str $100 name $24 sex $1;
if not prx1 then prx1 + prxparse("/(\D+)(M|F)(\d\d)(\d+\.\d)(\S+)/");
input str;
if prxmatch(prx1,str) then do;
    name = prxposn(prx1,1,str);
    sex = prxposn(prx1,2,str);
    age = input(prxposn(prx1,3,str), best.);
    weight = input(prxposn(prx1,4,str), best.);
    height = input(prxposn(prx1,5,str), best.);
    end;
drop prx1;
datalines;
JanetF1562.5112.5
MaryF1566.5112
CarolF1462.8102.5
JudyF1464.390
;

proc print data=test noobs; run;
PG
ramm
Calcite | Level 5
Thanks so much, PG ! Will work pn understanding PRX now.
PGStats
Opal | Level 21

You are welcome. Here is a description of the pattern, to get you started

 

"/(\D+)(M|F)(\d\d)(\d+\.\d)(\S+)/"

One or more non-digits, followed by

M or F, followed by

two digits, followed by

one or more digits, followed by a period, followed by a single digit, followed by

one or more non-space characters

 

each pair of parentheses defines a capture buffer (to be extracted with function prxposn)

 

Good luck! 

PG
RW9
Diamond | Level 26 RW9
Diamond | Level 26

More importantly, why do you have a file where you would be guessing which parts are data.  You can fix it by fixing width as examples given above by @PGStats.  However, what if those assumptions do not work for all data, or it changes.  Personally I would go back to the vendor and ask them to provide an easy to use robust file.  Correction at the source of issues will save you time in the long run.

ramm
Calcite | Level 5
Thanks, RW. I am new to SAS and am trying to solve a few challenging problems as practice. This question was part of that exercise. I will keep your advice in mind for real time scenarios.

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
  • 5 replies
  • 810 views
  • 1 like
  • 3 in conversation