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

I have a data set with a lot of variables with the year and month as variable name like 2014-01,2019-01,2019-02,2019-05,2020-01,2020-03,2021-01,2021-02,2021-03, 2021-04, 2021-05. I would like to sum the total of 2014-2020. I tried code below but get the error The sum functio call does not have enough argumensts. What do i do wrong?

 

Eerder_2021 = (Sum(of _201:)+sum(of _2020:));
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @rgjpot,

 

You will need to use the notation of SAS name literals:

Eerder_2021 = sum(of '201'n:, of '2020'n:);

View solution in original post

9 REPLIES 9
ChrisNZ
Tourmaline | Level 20

Show us the proc contents of your table please so we can see the variable names.

rgjpot
Calcite | Level 5

Dear Cris,

 

Thanks for the reply. As requested the proc contents

 

The CONTENTS Procedure

 Data Set NameObservationsMember TypeVariablesEngineIndexesCreatedObservation LengthLast ModifiedDeleted ObservationsProtectionCompressedData Set TypeSortedLabel Data Representation Encoding 
GD.GLIMS_DATA_2021_MERGE30169
DATA27
V90
16-07-2021 09:54:27264
16-07-2021 09:54:270
 NO
 NO
  
WINDOWS_64 
wlatin1 Western (Windows) 
 Engine/Host Dependent InformationData Set Page SizeNumber of Data Set PagesFirst Data PageMax Obs per PageObs in First Data PageNumber of Data Set RepairsExtendObsCounterFilenameRelease CreatedHost CreatedOwner NameFile SizeFile Size (bytes)
65536
122
1
248
232
0
YES
H:\Mijn Documenten\Finacien\mi\Mi 2021\Glims\glims_data_2021_merge.sas7bdat
9.0401M6
X64_7PRO
DS\rpot
8MB
8060928
 Alphabetic List of Variables and Attributes# Variable Type Len Format Informat Label789101112131415161718192021222324252627162354
2019-01Num8BEST17. 2019-01
2019-02Num8BEST3. 2019-02
2019-03Num8BEST3. 2019-03
2019-04Num8BEST3. 2019-04
2019-05Num8BEST3. 2019-05
2019-08Num8BEST3. 2019-08
2019-10Num8BEST3. 2019-10
2019-11Num8BEST3. 2019-11
2020-01Num8BEST3. 2020-01
2020-02Num8BEST3. 2020-02
2020-03Num8BEST3. 2020-03
2020-05Num8BEST3. 2020-05
2020-06Num8BEST3. 2020-06
2020-07Num8BEST3. 2020-07
2020-08Num8BEST3. 2020-08
2020-09Num8BEST3. 2020-09
2020-10Num8BEST3. 2020-10
2020-11Num8BEST3. 2020-11
2020-12Num8BEST3. 2020-12
2021-01Num8BEST5. 2021-01
2021-02Num8BEST5. 2021-02
AanvragerTypeChar12$12.$12.AanvragerType
Bedrag(ODV/Trial)Char23$23.$23.Bedrag(ODV/Trial)
CodeAanvragerChar14$14.$14.CodeAanvrager
KostenplaatsChar14$14.$14.Kostenplaats
RequestcodeChar21$21.$21.Requestcode
TariefChar12$12.$12.Tarief
Oligolas
Barite | Level 11

These are invalid variable names, since they start with a digit. Did you import them from an excel sheet?

________________________

- Cheers -

Oligolas
Barite | Level 11

Hi,

With your variable names fixed, this is how I'd expect your dataset to be.

And in your request you specify 2021 but your code only adds up to 2020.

%MACRO doit();
DATA have;
length count 8;
%let count=0;
%do i=14 %to 21;
   %do j=1 %to 12;
      _20%sysfunc(putN(&i,z2))_%sysfunc(putN(&j,z2))=1;
      %let count=%eval(&count.+1);
   %end;
%end;
count=&count;
output;
RUN;
%MEND doit;
%doit();

DATA want;
   length Eerder_2021 8;
   set have;
   Eerder_2021 = (Sum(of _201:)+sum(of _2020:));
RUN;

 

________________________

- Cheers -

rgjpot
Calcite | Level 5
thank you for your reply. I am relativly new in programmming. I indeed do not have _201x or _2020 columns in my dataset.

But i tought that when using the _ before the number of the variable name it means that it is a varible name and not a number.
What i want to now is de total before 2021 (Eerder dan 2021 in dutch).
FreelanceReinh
Jade | Level 19

Hello @rgjpot,

 

You will need to use the notation of SAS name literals:

Eerder_2021 = sum(of '201'n:, of '2020'n:);
rgjpot
Calcite | Level 5
This works great thank you.
Oligolas
Barite | Level 11

My suggestion is that you rename your variables with valid variables names before any further processing. (_2020_01 instead of 2020-01) 

Based on the results of your proc contents this is how I would do it here:

DATA have1;
   set GD.GLIMS_DATA_2021_MERGE;
RUN;

DATA _NULL_;
   set sashelp.vcolumn(keep=libname memname name) end=last;
   where libname eq 'WORK' 
   and memname eq 'HAVE1'
   and prxmatch('/^[0-9]/',name);
   length newname $32;
   newname=cats('_',translate(name,'_','-'));
   if _N_ eq 1 then call execute('DATA HAVE2; SET HAVE1; rename ');
   call execute("'"||strip(name)||"'n="||strip(newname));
   if last then call execute(';RUN;');
RUN;

DATA want;
   length Eerder_2021 8;
   set HAVE2;
   Eerder_2021 = (Sum(of _201:)+sum(of _2020:));
RUN;

 

________________________

- Cheers -

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 9 replies
  • 2048 views
  • 2 likes
  • 4 in conversation