DATA Step, Macro, Functions and more

Modify the input data

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

Modify the input data

hi!! I able to read my text file using snipets in SAS university edition but struggling to modify the data.


Accepted Solutions
Solution
‎11-13-2016 11:10 AM
Trusted Advisor
Posts: 1,405

Re: Modify the input data

You could save a lot of time if you had post your log and your output.

I'm using too SAS UE and this is not an issue.

 

Relating to your code:

PROC IMPORT DATAFILE="/folders/myshortcuts/My_Folders/New Text Document.txt"
OUT=WORK.Cont;
DBMS=txt;
REPLACE
RUN;

each row is imported as one observation without parsing the data into variables.

that is because you have no delimiters between the variables.

you can't use space as delimiter unless you want first name and last name separated.

that's why I asked:

How would you distinct between first name and last name in case of name contains 3 strings ?

Now relating to your first trial code:

DATA contest;
INFILE ’/folders/myshortcuts/My_Folders/New Text Document’;
INPUT Name $16. Age 3. +1 Type $1. +1 Date MMDDYY10.
(Score1 Score2 Score3 Score4 Score5) (4.1);
RUN;
PROC PRINT DATA = contest;
TITLE ’Pumpkin Carving Contest’;

when I run it I got 3 rows only.

to overcome it you need add TRUNCOVER option to the INFILE statement:

    

INFILE ’/folders/myshortcuts/My_Folders/New Text Document’ truncover;

another issue - in case the name is shorter then 16 characters, the age come to be part of the name.

check the log - there are notes starting: Invalid data for ... 

once again, that is because data miss delimiters between variables.

 

My solution:

enter commas between the variables, define delimiter=',' in the infile and add truncover option,

as in:

data my_data;
length name $16 datex $10; infile datalines dlm=',' truncover ; input Name $ age $ type $ datex $ score1-score5 4.1 ;
date = input(datex, mmddyy10.); drop datex; format date mmddyy10.; datalines; Alicia Grossman, 13, c, 10-28-2008, 7.8, 6.5, 7.2, 8.0, 7.9 Matthew Lee, 9, D, 10-30-2008, 6.5, 5.9, 6.8, 6.0, 8.1 Elizabeth Garcia, 10, C, 10-29-2008, 8.9, 7.9, 8.5, 9.0, 8.8 Lori Newcombe, 6, D, 10-30-2008, 6.7, 5.6, 4.9, 5.2, 6.1 Jose Martinez, 7, d, 10-31-2008, 8.9, 9.5, 10.0, 9.7, 9.0 Brian Williams, 11, C, 10-29-2008, 7.8, 8.4, 8.5, 7.9, 8.0 ; run;

View solution in original post


All Replies
PROC Star
Posts: 554

Re: Modify the input data

Wellcome to the forum Smiley Happy

 

You have to be more specific than that, what is your exact problem?

 

Regards

Contributor
Posts: 21

Re: Modify the input data

Hi,its simple i have my raw data in my folder and i print it easily but i want in a formatted way. How i import raw data and make some modification like read it as formatted way.      

Example:

Alicia Grossman 13 c 10-28-2008 7.8 6.5 7.2 8.0 7.9
Matthew Lee 9 D 10-30-2008 6.5 5.9 6.8 6.0 8.1
Elizabeth Garcia 10 C 10-29-2008 8.9 7.9 8.5 9.0 8.8
Lori Newcombe 6 D 10-30-2008 6.7 5.6 4.9 5.2 6.1
Jose Martinez 7 d 10-31-2008 8.9 9.510.0 9.7 9.0
Brian Williams 11 C 10-29-2008 7.8 8.4 8.5 7.9 8.0

Above data in a formatted way.

I am using sas university version 9.2 

 

 

Contributor
Posts: 21

Re: Modify the input data

Data:

Alicia Grossman 13 c 10-28-2008 7.8 6.5 7.2 8.0 7.9
Matthew Lee 9 D 10-30-2008 6.5 5.9 6.8 6.0 8.1
Elizabeth Garcia 10 C 10-29-2008 8.9 7.9 8.5 9.0 8.8
Lori Newcombe 6 D 10-30-2008 6.7 5.6 4.9 5.2 6.1.
Jose Martinez 7 d 10-31-2008 8.9 9.510.0 9.7 9.0
Brian Williams 11 C 10-29-2008 7.8 8.4 8.5 7.9 8.0

 

I able to print the above data but i wanted in a formated way.Please help me out

I am using SAS university version 

Trusted Advisor
Posts: 1,405

Re: Modify the input data

Please post:

1) your code to read the text file. Did you save it as sas dataset :

2) your code trying to modify the dadset ? the text file ? 

    and the log of running this code. 

    describe what modification you want to do.

Trusted Advisor
Posts: 1,405

Re: Modify the input data

[ Edited ]

How would you distinct between first name and last name in case of name contains 3 strings ?

You better have a delimiter between the variables like comma or other character.

You want it formated - give an example.

 

data my_data;

        infile datalines;

        input  FName $ LName $ var1 $ date_x mmffyy10. var3 var4 var5 var6 best 4.1;

       format date_x  date9.;

datalines;

Alicia Grossman 13 c 10-28-2008 7.8 6.5 7.2 8.0 7.9
Matthew Lee 9 D 10-30-2008 6.5 5.9 6.8 6.0 8.1
Elizabeth Garcia 10 C 10-29-2008 8.9 7.9 8.5 9.0 8.8
Lori Newcombe 6 D 10-30-2008 6.7 5.6 4.9 5.2 6.1
Jose Martinez 7 d 10-31-2008 8.9 9.510.0 9.7 9.0
Brian Williams 11 C 10-29-2008 7.8 8.4 8.5 7.9 8.0

;

run;

Contributor
Posts: 21

Re: Modify the input data

Issue is i am not able to import the file. I cleared with how to import the file using snippet but i need a formatted output which is in any format.

DATA contest;
INFILE ’/folders/myshortcuts/My_Folders/New Text Document’;
INPUT Name $16. Age 3. +1 Type $1. +1 Date MMDDYY10.
(Score1 Score2 Score3 Score4 Score5) (4.1);
RUN;
PROC PRINT DATA = contest;
TITLE ’Pumpkin Carving Contest’;

Contributor
Posts: 21

Re: Modify the input data

Issue is i am not able to import the file. I cleared with how to import the file using snippet but i need a formatted output which is in any format.

DATA contest;
INFILE ’/folders/myshortcuts/My_Folders/New Text Document’;
INPUT Name $16. Age 3. +1 Type $1. +1 Date MMDDYY10.
(Score1 Score2 Score3 Score4 Score5) (4.1);
RUN;
PROC PRINT DATA = contest;
TITLE ’Pumpkin Carving Contest’;

Contributor
Posts: 21

Re: Modify the input data

i am confusing you ??
Contributor
Posts: 21

Re: Modify the input data

im using sas university version
Trusted Advisor
Posts: 1,405

Re: Modify the input data

Assuming data contest satifies you, you can print it by:

 

proc print data=contest noobs;

  var name age type date score1-score5;

  format name $16. age 2. type $1. date mmddyy10. score1-scror5 4.1;

run;

 

that will give you a formated report .

Contributor
Posts: 21

Re: Modify the input data

/** Import an Txt file. **/
PROC IMPORT DATAFILE="/folders/myshortcuts/My_Folders/New Text Document.txt"
OUT=WORK.Cont;
DBMS=txt;
REPLACE
RUN;

/** Print the results**/

PROC PRINT DATA=work.Cont;
RUN;
this is my import txt snippet where i can write the program??
Solution
‎11-13-2016 11:10 AM
Trusted Advisor
Posts: 1,405

Re: Modify the input data

You could save a lot of time if you had post your log and your output.

I'm using too SAS UE and this is not an issue.

 

Relating to your code:

PROC IMPORT DATAFILE="/folders/myshortcuts/My_Folders/New Text Document.txt"
OUT=WORK.Cont;
DBMS=txt;
REPLACE
RUN;

each row is imported as one observation without parsing the data into variables.

that is because you have no delimiters between the variables.

you can't use space as delimiter unless you want first name and last name separated.

that's why I asked:

How would you distinct between first name and last name in case of name contains 3 strings ?

Now relating to your first trial code:

DATA contest;
INFILE ’/folders/myshortcuts/My_Folders/New Text Document’;
INPUT Name $16. Age 3. +1 Type $1. +1 Date MMDDYY10.
(Score1 Score2 Score3 Score4 Score5) (4.1);
RUN;
PROC PRINT DATA = contest;
TITLE ’Pumpkin Carving Contest’;

when I run it I got 3 rows only.

to overcome it you need add TRUNCOVER option to the INFILE statement:

    

INFILE ’/folders/myshortcuts/My_Folders/New Text Document’ truncover;

another issue - in case the name is shorter then 16 characters, the age come to be part of the name.

check the log - there are notes starting: Invalid data for ... 

once again, that is because data miss delimiters between variables.

 

My solution:

enter commas between the variables, define delimiter=',' in the infile and add truncover option,

as in:

data my_data;
length name $16 datex $10; infile datalines dlm=',' truncover ; input Name $ age $ type $ datex $ score1-score5 4.1 ;
date = input(datex, mmddyy10.); drop datex; format date mmddyy10.; datalines; Alicia Grossman, 13, c, 10-28-2008, 7.8, 6.5, 7.2, 8.0, 7.9 Matthew Lee, 9, D, 10-30-2008, 6.5, 5.9, 6.8, 6.0, 8.1 Elizabeth Garcia, 10, C, 10-29-2008, 8.9, 7.9, 8.5, 9.0, 8.8 Lori Newcombe, 6, D, 10-30-2008, 6.7, 5.6, 4.9, 5.2, 6.1 Jose Martinez, 7, d, 10-31-2008, 8.9, 9.5, 10.0, 9.7, 9.0 Brian Williams, 11, C, 10-29-2008, 7.8, 8.4, 8.5, 7.9, 8.0 ; run;
Super User
Posts: 17,960

Re: Modify the input data

There are SAS tutorials here:

http://support.sas.com/training/tutorial/

 

In your particular case, you probably want to create a SAS program and then work off that. It sounds like you may be working in the Visual Programmer side?

 

 

Contributor
Posts: 21

Re: Modify the input data

Sir,

I not yet get the output.If there any problem in my code or the path is wrong  

DATA contest;
INFILE ’/folders/myshortcuts/My_Folders/New.txt’ truncover;
INPUT Name $16. Age 3. +1 Type $1. +1 Date MMDDYY10.
(Score1 Score2 Score3 Score4 Score5) (4.1);
RUN;
PROC PRINT DATA = contest;
TITLE ’Pumpkin Carving Contest’;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 20 replies
  • 685 views
  • 3 likes
  • 4 in conversation