BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jfaruqui
Obsidian | Level 7

guys ... 

stuck on a small basic problem .. 

need to write to a file but using values from subsequent observations .. 

the attached image explains the issue .. please help with code to replace "????????" part ..

 

thanks 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Something like this?

 

data test;
input ID Name$ Month$ Amount;
infile datalines dlm=',' dsd;
cards;
1001,Jenny,JAN,300
1001,,JUL,550
1001,,DEC,275
1002,George,AUG,100
1003,Frank,FEB,375
1003,,NOV,460
;

data _null_;
   set test;
   by ID;
   file 'Yourpath\Donations.txt';
   if first.ID then put "Hi " Name
   / "Your donation(s) are as follows"
   / Month Amount dollar10.2;
   if not first.ID then put Month Amount dollar10.2;
run;

View solution in original post

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

So the window is what you want the txt file to look like, correct?

 

Can you post the code here so we can provide a code answer?

jfaruqui
Obsidian | Level 7

 

sure ...

 

data _null_;
infile datalines;
input ID Name$ 6-13 Month$ Amount;
file '/folders/myfolders/Donations.txt' print;
PUT
'Hi' name
//'Your donation(s) are as follows:'
// ???????;
put _page_;

cards;
1001 Jenny   JAN 300
1001         JUL 550
1001         DEC 275
1002 George  AUG 100
1003 Frank   FEB 375
1003         NOV 460
;

PeterClemmensen
Tourmaline | Level 20

Is this what your actual data looks like? This is probably not the data you want to work with?

 

data test;
input ID Name$ Month$ Amount;
cards;
1001 Jenny JAN 300
1001 JUL 550
1001 DEC 275
1002 George AUG 100
1003 Frank FEB 375
1003 NOV 460
;

 I'm guessing your data looks more like this?

data test;
input ID Name$ Month$ Amount;
infile datalines dlm=',' dsd;
cards;
1001,Jenny,JAN,300
1001,,JUL,550
1001,,DEC,275
1002,George,AUG,100
1003,Frank,FEB,375
1003,,NOV,460
;
jfaruqui
Obsidian | Level 7

looks more like this ... copy/paste is screwing up the spacing 

i wouldn't mind making a dataset with this data and then using the dataset to generate the txt files either ... so we can use the BY statement (in case first. and last. needs to be used)

 

----x----1----x----2----x----3
1001 Jenny   JAN 300
1001         JUL 550
1001         DEC 275
1002 George  AUG 100
1003 Frank   FEB 375
1003         NOV 460
;

jfaruqui
Obsidian | Level 7
that works too bro ... its not the data entry that is tripping me up .. its what to write in the PUT statement that allows me to create reports for the 3 users with their donation months and amounts ..
PeterClemmensen
Tourmaline | Level 20

Something like this?

 

data test;
input ID Name$ Month$ Amount;
infile datalines dlm=',' dsd;
cards;
1001,Jenny,JAN,300
1001,,JUL,550
1001,,DEC,275
1002,George,AUG,100
1003,Frank,FEB,375
1003,,NOV,460
;

data _null_;
   set test;
   by ID;
   file 'Yourpath\Donations.txt';
   if first.ID then put "Hi " Name
   / "Your donation(s) are as follows"
   / Month Amount dollar10.2;
   if not first.ID then put Month Amount dollar10.2;
run;
Shmuel
Garnet | Level 18

Next time post your code within the man-icon window.

 

Replace the '????...?' with: 

month $3.  amount 7.2 /

but you still need to recognize a start of a new name, so your code need be -

Suppose you read your input into sas dataset named 'have':

 

data _null_;
  set  have;
         file .... /* your code */
         if not missing(name) then 
            put 'Hi ' name // 'Your do.....: '    /* your text */
                / month $3.  amount dollar7.2 ;
        else   put / mont $3.  amount dollar7.2;
run;

 

  

jfaruqui
Obsidian | Level 7

thanks all !!!

I love this site and these boards !!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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